forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminModel.hpp
449 lines (342 loc) · 14.5 KB
/
AdminModel.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <cmath>
#include <ctime>
#include <list>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <common/Log.hpp>
#include "Util.hpp"
#include "net/WebSocketHandler.hpp"
struct DocumentAggregateStats;
/// A client view in Admin controller.
class View
{
public:
View(std::string sessionId, std::string userName, std::string userId)
: _sessionId(std::move(sessionId))
, _userName(std::move(userName))
, _userId(std::move(userId))
, _start(std::time(nullptr))
, _loadDuration(0)
{
}
void expire() { _end = std::time(nullptr); }
std::string getUserName() const { return _userName; }
std::string getUserId() const { return _userId; }
std::string getSessionId() const { return _sessionId; }
bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; }
std::chrono::milliseconds getLoadDuration() const { return _loadDuration; }
void setLoadDuration(std::chrono::milliseconds loadDuration) { _loadDuration = loadDuration; }
private:
const std::string _sessionId;
const std::string _userName;
const std::string _userId;
const std::time_t _start;
std::time_t _end = 0;
std::chrono::milliseconds _loadDuration;
};
struct DocCleanupSettings
{
void setEnable(bool enable) { _enable = enable; }
bool getEnable() const { return _enable; }
void setCleanupInterval(size_t cleanupInterval) { _cleanupInterval = cleanupInterval; }
size_t getCleanupInterval() const { return _cleanupInterval; }
void setBadBehaviorPeriod(size_t badBehaviorPeriod) { _badBehaviorPeriod = badBehaviorPeriod; }
size_t getBadBehaviorPeriod() const { return _badBehaviorPeriod; }
void setIdleTime(size_t idleTime) { _idleTime = idleTime; }
size_t getIdleTime() { return _idleTime; }
void setLimitDirtyMem(size_t limitDirtyMem) { _limitDirtyMem = limitDirtyMem; }
size_t getLimitDirtyMem() const { return _limitDirtyMem; }
void setLimitCpu(size_t limitCpu) { _limitCpu = limitCpu; }
size_t getLimitCpu() const { return _limitCpu; }
void setLostKitGracePeriod(size_t lostKitGracePeriod) { _lostKitGracePeriod = lostKitGracePeriod; }
size_t getLostKitGracePeriod() { return _lostKitGracePeriod; }
private:
bool _enable;
size_t _cleanupInterval;
size_t _badBehaviorPeriod;
size_t _idleTime;
size_t _limitDirtyMem;
size_t _limitCpu;
size_t _lostKitGracePeriod;
};
struct DocProcSettings
{
void setLimitVirtMemMb(size_t limitVirtMemMb) { _limitVirtMemMb = limitVirtMemMb; }
size_t getLimitVirtMemMb() const { return _limitVirtMemMb; }
void setLimitStackMemKb(size_t limitStackMemKb) { _limitStackMemKb = limitStackMemKb; }
size_t getLimitStackMemKb() const { return _limitStackMemKb; }
void setLimitFileSizeMb(size_t limitFileSizeMb) { _limitFileSizeMb = limitFileSizeMb; }
size_t getLimitFileSizeMb() const { return _limitFileSizeMb; }
void setLimitNumberOpenFiles(size_t limitNumberOpenFiles) { _limitNumberOpenFiles = limitNumberOpenFiles; }
size_t getLimitNumberOpenFiles() const { return _limitNumberOpenFiles; }
DocCleanupSettings& getCleanupSettings() { return _docCleanupSettings; }
private:
size_t _limitVirtMemMb;
size_t _limitStackMemKb;
size_t _limitFileSizeMb;
size_t _limitNumberOpenFiles;
DocCleanupSettings _docCleanupSettings;
};
/// Containing basic information about document
class DocBasicInfo
{
std::string _docKey;
std::time_t _idleTime;
int _mem;
bool _saved;
public:
DocBasicInfo(std::string docKey, std::time_t idleTime, int mem, bool saved)
: _docKey(std::move(docKey))
, _idleTime(idleTime)
, _mem(mem)
, _saved(saved)
{
}
const std::string& getDocKey() const { return _docKey; }
std::time_t getIdleTime() const { return _idleTime; }
int getMem() const { return _mem; }
bool getSaved() const { return _saved; }
};
/// A document in Admin controller.
class Document
{
// cf. FILE* member.
Document(const Document &) = delete;
Document& operator = (const Document &) = delete;
public:
Document(std::string docKey, pid_t pid, std::string filename, std::string wopiHost)
: _docKey(std::move(docKey))
, _pid(pid)
, _activeViews(0)
, _filename(std::move(filename))
, _wopiHost(std::move(wopiHost))
, _memoryDirty(0)
, _lastJiffy(0)
, _lastCpuPercentage(0)
, _start(std::time(nullptr))
, _lastActivity(_start)
, _end(0)
, _sentBytes(0)
, _recvBytes(0)
, _wopiDownloadDuration(0)
, _wopiUploadDuration(0)
, _procSMaps(nullptr)
, _lastTimeSMapsRead(0)
, _isModified(false)
, _hasMemDirtyChanged(true)
, _badBehaviorDetectionTime(0)
, _abortTime(0)
{
}
~Document()
{
if (_procSMaps)
fclose(_procSMaps);
}
std::string getDocKey() const { return _docKey; }
pid_t getPid() const { return _pid; }
std::string getFilename() const { return _filename; }
std::string getHostName() const { return _wopiHost; }
bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; }
std::time_t getElapsedTime() const { return std::time(nullptr) - _start; }
std::time_t getIdleTime() const { return std::time(nullptr) - _lastActivity; }
void addView(const std::string& sessionId, const std::string& userName, const std::string& userId);
int expireView(const std::string& sessionId);
unsigned getActiveViews() const { return _activeViews; }
size_t getLastJiffies() const { return _lastJiffy; }
void setLastJiffies(size_t newJ);
unsigned getLastCpuPercentage(){ return _lastCpuPercentage; }
const std::map<std::string, View>& getViews() const { return _views; }
void updateLastActivityTime() { _lastActivity = std::time(nullptr); }
void updateMemoryDirty();
size_t getMemoryDirty() const { return _memoryDirty; }
std::pair<std::time_t, std::string> getSnapshot() const;
const std::string getHistory() const;
void takeSnapshot();
void setModified(bool value) { _isModified = value; }
bool getModifiedStatus() const { return _isModified; }
void addBytes(uint64_t sent, uint64_t recv)
{
_sentBytes += sent;
_recvBytes += recv;
}
std::time_t getOpenTime() const { return isExpired() ? _end - _start : getElapsedTime(); }
uint64_t getSentBytes() const { return _sentBytes; }
uint64_t getRecvBytes() const { return _recvBytes; }
void setViewLoadDuration(const std::string& sessionId, std::chrono::milliseconds viewLoadDuration);
void setWopiDownloadDuration(std::chrono::milliseconds wopiDownloadDuration) { _wopiDownloadDuration = wopiDownloadDuration; }
std::chrono::milliseconds getWopiDownloadDuration() const { return _wopiDownloadDuration; }
void setWopiUploadDuration(const std::chrono::milliseconds wopiUploadDuration) { _wopiUploadDuration = wopiUploadDuration; }
std::chrono::milliseconds getWopiUploadDuration() const { return _wopiUploadDuration; }
void setProcSMapsFD(const int smapsFD) { _procSMaps = fdopen(smapsFD, "r"); }
bool hasMemDirtyChanged() const { return _hasMemDirtyChanged; }
void setMemDirtyChanged(bool changeStatus) { _hasMemDirtyChanged = changeStatus; }
time_t getBadBehaviorDetectionTime() const { return _badBehaviorDetectionTime; }
void setBadBehaviorDetectionTime(time_t badBehaviorDetectionTime){ _badBehaviorDetectionTime = badBehaviorDetectionTime;}
time_t getAbortTime() const { return _abortTime; }
void setAbortTime(time_t abortTime) { _abortTime = abortTime; }
std::string to_string() const;
private:
const std::string _docKey;
const pid_t _pid;
/// SessionId mapping to View object
std::map<std::string, View> _views;
/// Total number of active views
unsigned _activeViews;
/// Hosted filename
std::string _filename;
std::string _wopiHost;
/// The dirty (ie. un-shared) memory of the document's Kit process.
size_t _memoryDirty;
/// Last noted Jiffy count
size_t _lastJiffy;
std::chrono::steady_clock::time_point _lastJiffyTime;
unsigned _lastCpuPercentage;
std::time_t _start;
std::time_t _lastActivity;
std::time_t _end;
std::map<std::time_t,std::string> _snapshots;
/// Total bytes sent and recv'd by this document.
uint64_t _sentBytes, _recvBytes;
//Download/upload duration from/to storage for this document
std::chrono::milliseconds _wopiDownloadDuration;
std::chrono::milliseconds _wopiUploadDuration;
FILE* _procSMaps;
std::time_t _lastTimeSMapsRead;
bool _isModified;
bool _hasMemDirtyChanged;
std::time_t _badBehaviorDetectionTime;
std::time_t _abortTime;
};
/// An Admin session subscriber.
class Subscriber
{
public:
explicit Subscriber(std::weak_ptr<WebSocketHandler> ws)
: _ws(std::move(ws))
, _start(std::time(nullptr))
{
LOG_INF("Subscriber ctor.");
}
~Subscriber()
{
LOG_INF("Subscriber dtor.");
}
bool notify(const std::string& message);
bool subscribe(const std::string& command);
void unsubscribe(const std::string& command);
void expire() { _end = std::time(nullptr); }
bool isExpired() const { return _end != 0 && std::time(nullptr) >= _end; }
private:
/// The underlying AdminRequestHandler
std::weak_ptr<WebSocketHandler> _ws;
std::set<std::string> _subscriptions;
std::time_t _start;
std::time_t _end = 0;
};
/// The Admin controller implementation.
class AdminModel
{
AdminModel(const AdminModel &) = delete;
AdminModel& operator = (const AdminModel &) = delete;
public:
AdminModel() :
_segFaultCount(0),
_owner(std::this_thread::get_id())
{
LOG_INF("AdminModel ctor.");
}
~AdminModel();
/// All methods here must be called from the Admin socket-poll
void setThreadOwner(const std::thread::id &id) { _owner = id; }
/// In debug mode check that code is running in the correct thread.
/// Asserts in the debug builds, otherwise just logs.
void assertCorrectThread() const;
std::string query(const std::string& command);
std::string getAllHistory() const;
/// Returns memory consumed by all active coolkit processes
unsigned getKitsMemoryUsage();
size_t getKitsJiffies();
void subscribe(int sessionId, const std::weak_ptr<WebSocketHandler>& ws);
void subscribe(int sessionId, const std::string& command);
void unsubscribe(int sessionId, const std::string& command);
void modificationAlert(const std::string& docKey, pid_t pid, bool value);
void clearMemStats() { _memStats.clear(); }
void clearCpuStats() { _cpuStats.clear(); }
void addMemStats(unsigned memUsage);
void addCpuStats(unsigned cpuUsage);
void addSentStats(uint64_t sent);
void addRecvStats(uint64_t recv);
void setCpuStatsSize(unsigned size);
void setMemStatsSize(unsigned size);
void notify(const std::string& message);
void addDocument(const std::string& docKey, pid_t pid, const std::string& filename,
const std::string& sessionId, const std::string& userName, const std::string& userId,
const int smapsFD, const std::string& URI);
void removeDocument(const std::string& docKey, const std::string& sessionId);
void removeDocument(const std::string& docKey);
void updateLastActivityTime(const std::string& docKey);
void addBytes(const std::string& docKey, uint64_t sent, uint64_t recv);
uint64_t getSentBytesTotal() { return _sentBytesTotal; }
uint64_t getRecvBytesTotal() { return _recvBytesTotal; }
static double getServerUptimeSecs();
/// Document basic info list sorted by most idle time
std::vector<DocBasicInfo> getDocumentsSortedByIdle() const;
void cleanupResourceConsumingDocs();
void setViewLoadDuration(const std::string& docKey, const std::string& sessionId, std::chrono::milliseconds viewLoadDuration);
void setDocWopiDownloadDuration(const std::string& docKey, std::chrono::milliseconds wopiDownloadDuration);
void setDocWopiUploadDuration(const std::string& docKey, const std::chrono::milliseconds wopiUploadDuration);
void addSegFaultCount(unsigned segFaultCount);
void setForKitPid(pid_t pid) { _forKitPid = pid; }
void addLostKitsTerminated(unsigned lostKitsTerminated);
void getMetrics(std::ostringstream &oss);
std::set<pid_t> getDocumentPids() const;
void UpdateMemoryDirty();
void notifyDocsMemDirtyChanged();
const DocProcSettings& getDefDocProcSettings() const { return _defDocProcSettings; }
void setDefDocProcSettings(const DocProcSettings& docProcSettings) { _defDocProcSettings = docProcSettings; }
static int getPidsFromProcName(const std::regex& procNameRegEx, std::vector<int> *pids);
static int getAssignedKitPids(std::vector<int> *pids);
static int getUnassignedKitPids(std::vector<int> *pids);
static int getKitPidsFromSystem(std::vector<int> *pids);
private:
void doRemove(std::map<std::string, std::unique_ptr<Document>>::iterator &docIt);
std::string getMemStats();
std::string getSentActivity();
std::string getRecvActivity();
std::string getCpuStats();
unsigned getTotalActiveViews();
std::string getDocuments() const;
void CalcDocAggregateStats(DocumentAggregateStats& stats);
private:
std::map<int, Subscriber> _subscribers;
std::map<std::string, std::unique_ptr<Document>> _documents;
std::map<std::string, std::unique_ptr<Document>> _expiredDocuments;
/// The last N total memory Dirty size.
std::list<unsigned> _memStats;
unsigned _memStatsSize = 100;
std::list<unsigned> _cpuStats;
unsigned _cpuStatsSize = 100;
std::list<unsigned> _sentStats;
unsigned _sentStatsSize = 200;
std::list<unsigned> _recvStats;
unsigned _recvStatsSize = 200;
uint64_t _sentBytesTotal = 0;
uint64_t _recvBytesTotal = 0;
uint64_t _segFaultCount = 0;
uint64_t _lostKitsTerminatedCount = 0;
pid_t _forKitPid = 0;
/// We check the owner even in the release builds, needs to be always correct.
std::thread::id _owner;
DocProcSettings _defDocProcSettings;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */