-
Notifications
You must be signed in to change notification settings - Fork 3
/
modified_sync.patch
832 lines (792 loc) · 31 KB
/
modified_sync.patch
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
Index: ChronoVisor/src/ChronoVisorServer.cpp
===================================================================
diff --git a/ChronoVisor/src/ChronoVisorServer.cpp b/ChronoVisor/src/ChronoVisorServer.cpp
--- a/ChronoVisor/src/ChronoVisorServer.cpp (revision d74b66d610b20abfbafd63df5fed4bda534b008e)
+++ b/ChronoVisor/src/ChronoVisorServer.cpp (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -3,34 +3,34 @@
//
#include <algorithm>
-#include <stdio.h>
+#include <cstdio>
#include "ChronoVisorServer.h"
+#include "ClocksourceManager.h"
+#include "common.h"
namespace ChronoVisor {
ChronoVisorServer::ChronoVisorServer(int port)
: SocketPP::TCPServer(port) {
timeDBUpdateInterval_ = TIME_DB_UPDATE_INTERVAL;
+ pTimeManager = new TimeManager();
initTimeDB();
initSerDe();
- startTimeManagerThread();
+ startPeriodicTimeDBUpdateThread();
+ startOnDemandTimeDBUpdateThread();
}
ChronoVisorServer::ChronoVisorServer(int port, int update_interval)
: SocketPP::TCPServer(port), timeDBUpdateInterval_(update_interval) {
+ pTimeManager = new TimeManager(update_interval);
initTimeDB();
initSerDe();
- startTimeManagerThread();
+ startPeriodicTimeDBUpdateThread();
+ startOnDemandTimeDBUpdateThread();
}
- TimeRecord ChronoVisorServer::getCurTimeInfo() {
- std::string record_str;
- timeDB_->Get(rocksdb::ReadOptions(), std::to_string(lastTimestamp_).c_str(), &record_str);
-// LOGD("get time record from timeDB: %s", record_str.c_str());
- TimeRecord record;
- record.drift_rate_ = std::stod(record_str);
- record.updateTimestamp();
- return record;
+ TimeRecord ChronoVisorServer::getCurTimeInfo() {
+ return *(pTimeManager->genOnDemandTimeRecord());
}
void ChronoVisorServer::addChronoKeeperInfo(const ChronoKeeperInfo& chronoKeeperInfo) {
@@ -89,42 +89,86 @@
clientRegistry_.erase(iter);
}
- void ChronoVisorServer::startTimeManagerThread() {
- timeManagerThread_ = new std::thread([this] {
- LOGD("timeManagerThread running...");
- TimeRecord record;
- lastTimestamp_ = record.getTimestamp();
+ void ChronoVisorServer::startPeriodicTimeDBUpdateThread() {
+ periodicTimeDBUpdateThread_ = new std::thread([this] {
+ LOGD("periodic TimeDB update thread running...");
+ TimeRecord record = *(pTimeManager->genPeriodicTimeRecord());
rocksdb::Status status = timeDB_->Put(rocksdb::WriteOptions(),
std::to_string(record.getTimestamp()).c_str(),
std::to_string(record.getDriftRate()).c_str()); // first record
- LOGD("big bang time record is generated, %s, appending to timeDB ...",
+ LOGI("big bang time record is generated, %s, appended to timeDB ...",
record.toString().c_str());
+ pTimeManager->refTimestampUpdateInterval_ = timeDBUpdateInterval_;
while (true) {
{
+ // Append a record every interval
+ record = *(pTimeManager->genPeriodicTimeRecord());
+
+ std::ostringstream out;
+ out.precision(10);
+ out << std::fixed << record.getDriftRate();
+ std::string driftRateStr = out.str();
+
+ // Get TimeDB lock
+ {
+ std::unique_lock<std::mutex> lock(timeDBMutex_);
+
+ // Write to TimeDB
+ status = timeDB_->Put(rocksdb::WriteOptions(),
+ std::to_string(record.getTimestamp()).c_str(),
+ driftRateStr.c_str());
+ }
+ LOGI("every %lf seconds, new time record %s is appended to timeDB ...", \
+ timeDBUpdateInterval_, record.toString().c_str());
+
+// LOGD("time record appended");
+
LOGD("sleeping for %lf seconds ...", timeDBUpdateInterval_);
usleep(timeDBUpdateInterval_ * 1000000);
- record.setUpdateInterval(timeDBUpdateInterval_);
- record.getCurTimeRecord(lastTimestamp_);
+ if (periodicThrdStopped_) return;
+ }
+ }
+ });
+ }
+ void ChronoVisorServer::startOnDemandTimeDBUpdateThread() {
+ onDemandTimeDBUpdateThread_ = new std::thread([this] {
+ LOGD("on-demand TimeDB update thread running...");
+ TimeRecord record;
+ rocksdb::Status status;
+ while (true) {
+ {
+ // Take a TimeRecord from the shared queue
{
- std::unique_lock<std::mutex> lock(timeDBMutex_);
- lastTimestamp_ = record.getTimestamp();
+ // Lock the queue
+ std::unique_lock<std::mutex> lock(timeDBWriteQueueMutex_);
+ while (timeDBWriteQueueSize_ == 0) timeDBWriteQueueCV_.wait(lock);
+ timeDBWriteQueueSize_--;
+
+ record = timeDBWriteQueue_.front();
+ timeDBWriteQueue_.pop();
}
std::ostringstream out;
out.precision(10);
out << std::fixed << record.getDriftRate();
std::string driftRateStr = out.str();
- status = timeDB_->Put(rocksdb::WriteOptions(),
- std::to_string(record.getTimestamp()).c_str(),
- driftRateStr.c_str());
-// LOGD("new time record %s is appended to timeDB ...", record.toString().c_str());
+
+ {
+ // Get TimeDB lock
+ std::unique_lock<std::mutex> lock(timeDBMutex_);
+
+ // Write to TimeDB
+ status = timeDB_->Put(rocksdb::WriteOptions(),
+ std::to_string(record.getTimestamp()).c_str(),
+ driftRateStr.c_str());
+ }
+ LOGI("upon client requests, new time record %s is appended to timeDB ...", \
+ record.toString().c_str());
-// LOGD("time record appended");
-
- if (stopped_) return;
+ if (onDemandThrdStopped_) return;
}
}
});
@@ -149,14 +193,22 @@
}
case ClientMessage::GETTIMEINFO: {
serverMsg.msgType_ = ServerMessage::GETTIMEINFORESPONSE;
- TimeRecord record = this->getCurTimeInfo();
- serverMsg.timeInfo_.timestamp_ = std::to_string(lastTimestamp_);
+ TimeRecord record = *(pTimeManager->genOnDemandTimeRecord());
+ serverMsg.timeInfo_.timestamp_ = std::to_string(record.getTimestamp());
serverMsg.timeInfo_.driftRate_ = record.getDriftRate();
{
oss = pSerDe->serializeServerMessage(serverMsg);
}
LOGD("Msg to client: %s", serverMsg.toString().c_str());
reply.rawMsg.initMsg((const byte *) oss->str().c_str(), oss->str().length());
+
+ // Upon each get_clock request from clients, append a record to TimeDB
+ {
+ std::unique_lock<std::mutex> lock(timeDBWriteQueueMutex_);
+ timeDBWriteQueueSize_++;
+ timeDBWriteQueueCV_.notify_all();
+ timeDBWriteQueue_.emplace(record);
+ }
break;
}
case ClientMessage::GETCHRONOKEEPERINFO: {
Index: ChronoVisor/src/ClocksourceManager.cpp
===================================================================
diff --git a/ChronoVisor/src/ClocksourceManager.cpp b/ChronoVisor/src/ClocksourceManager.cpp
new file mode 100644
--- /dev/null (revision 36d412b0663a9fdc0c31969c260f39a7e349f51e)
+++ b/ChronoVisor/src/ClocksourceManager.cpp (revision 36d412b0663a9fdc0c31969c260f39a7e349f51e)
@@ -0,0 +1,18 @@
+//
+// Created by kfeng on 2/8/22.
+//
+
+#include "ClocksourceManager.h"
+
+Clocksource *Clocksource::Create(ClocksourceType type) {
+ switch (type) {
+ case ClocksourceType::C_STYLE:
+ return new ClocksourceCStyle();
+ case ClocksourceType::CPP_STYLE:
+ return new ClocksourceCPPStyle();
+ case ClocksourceType::TSC:
+ return new ClocksourceTSC();
+ default:
+ return nullptr;
+ }
+}
\ No newline at end of file
Index: ChronoVisor/src/ClocksourceManager.h
===================================================================
diff --git a/ChronoVisor/src/ClocksourceManager.h b/ChronoVisor/src/ClocksourceManager.h
new file mode 100644
--- /dev/null (revision 36d412b0663a9fdc0c31969c260f39a7e349f51e)
+++ b/ChronoVisor/src/ClocksourceManager.h (revision 36d412b0663a9fdc0c31969c260f39a7e349f51e)
@@ -0,0 +1,101 @@
+//
+// Created by kfeng on 2/8/22.
+//
+
+#ifndef CHRONOLOG_CLOCKSOURCEMANAGER_H
+#define CHRONOLOG_CLOCKSOURCEMANAGER_H
+
+#include <chrono>
+#include <cstdint>
+#include <ctime>
+#include <unistd.h>
+#include <emmintrin.h>
+
+#define lfence() _mm_lfence()
+#define mfence() _mm_mfence()
+
+enum ClocksourceType {
+ TBD = 0,
+ C_STYLE = 1,
+ CPP_STYLE = 2,
+ TSC = 3
+};
+
+class Clocksource {
+public:
+ static Clocksource *Create(ClocksourceType type);
+ virtual ~Clocksource() = default;;
+ /**
+ * @name Get timestamp
+ */
+ ///@{
+ virtual uint64_t getTimestamp() = 0;
+ ///@}
+};
+
+class ClocksourceCStyle : public Clocksource {
+public:
+ uint64_t getTimestamp() override {
+ struct timespec t{};
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return (t.tv_sec * (uint64_t) 1e9 + t.tv_nsec);
+ }
+};
+
+class ClocksourceTSC : public Clocksource {
+public:
+ uint64_t getTimestamp() override {
+ unsigned int proc_id;
+ uint64_t t = __builtin_ia32_rdtscp(&proc_id);
+ lfence();
+ return t;
+ }
+};
+class ClocksourceCPPStyle : public Clocksource {
+public:
+ uint64_t getTimestamp() override {
+ using namespace std::chrono;
+ using clock = steady_clock;
+ clock::time_point t = clock::now();
+ return (t.time_since_epoch().count());
+ }
+};
+
+class ClocksourceManager {
+private:
+ ClocksourceManager() : clocksource_(nullptr), clocksourceType_(ClocksourceType::TBD) {}
+
+public:
+ ~ClocksourceManager() {
+ if (clocksource_) {
+ delete[] clocksource_;
+ clocksource_ = nullptr;
+ }
+ if (clocksourceManager_) {
+ delete[] clocksourceManager_;
+ clocksourceManager_ = nullptr;
+ }
+ }
+
+ static ClocksourceManager *getInstance() {
+ if (!clocksourceManager_) {
+ clocksourceManager_ = new ClocksourceManager();
+ }
+ return clocksourceManager_;
+ }
+
+ void setClocksourceType(ClocksourceType type) {
+ this->clocksourceType_ = type;
+ }
+
+ Clocksource *getClocksource() {
+ this->clocksource_ = Clocksource::Create(this->clocksourceType_);
+ return this->clocksource_;
+ }
+private:
+ static ClocksourceManager *clocksourceManager_;
+ Clocksource *clocksource_;
+ ClocksourceType clocksourceType_;
+};
+
+#endif //CHRONOLOG_CLOCKSOURCEMANAGER_H
Index: ChronoVisor/src/TimeRecord.cpp
===================================================================
diff --git a/ChronoVisor/src/TimeRecord.cpp b/ChronoVisor/src/TimeRecord.cpp
--- a/ChronoVisor/src/TimeRecord.cpp (revision d74b66d610b20abfbafd63df5fed4bda534b008e)
+++ b/ChronoVisor/src/TimeRecord.cpp (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -1,35 +1,3 @@
//
// Created by kfeng on 10/25/21.
-//
-
-#include <chrono>
-#include "TimeRecord.h"
-#include "log.h"
-
-TimeRecord TimeRecord::getCurTimeRecord(long last_timestamp) {
- timestamp_ = getCurTimestamp();
- drift_rate_ = calCurDriftRate(last_timestamp, update_interval_);
-
- return *this;
-}
-
-long TimeRecord::getCurTimestamp() {
- using namespace std::chrono;
-
- high_resolution_clock::time_point t = high_resolution_clock::now();
- nanoseconds epoch = t.time_since_epoch();
- long timestamp = epoch.count();
-// LOGD("timestamp: %ld", timestamp);
-
- return timestamp;
-}
-
-double TimeRecord::calCurDriftRate(long last_timestamp) {
- long cur = getCurTimestamp();
- return (cur - last_timestamp) / update_interval_ / 1000000000.0 - 1;
-}
-
-double TimeRecord::calCurDriftRate(long last_timestamp, double update_interval) {
- long cur = getCurTimestamp();
- return (cur - last_timestamp) / update_interval / 1000000000.0 - 1;
-}
\ No newline at end of file
+//
\ No newline at end of file
Index: ChronoVisor/test/Server/chronovisorserver_test.cpp
===================================================================
diff --git a/ChronoVisor/test/Server/chronovisorserver_test.cpp b/ChronoVisor/test/Server/chronovisorserver_test.cpp
--- a/ChronoVisor/test/Server/chronovisorserver_test.cpp (revision d74b66d610b20abfbafd63df5fed4bda534b008e)
+++ b/ChronoVisor/test/Server/chronovisorserver_test.cpp (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -14,7 +14,8 @@
const int port = 6000;
LOGI("ChronoVisor server port:%d", port);
- ChronoVisorServer server(port, 30);
+ ChronoVisorServer server(port, 10);
+ server.setClocksourceType(ClocksourceType::C_STYLE);
// server.setConnHandle([] (const SocketPP::TCPStream& stream) {
// LOGI("a client connected: fd=%d", stream.fd);
Index: ChronoVisor/include/TimeRecord.h
===================================================================
diff --git a/ChronoVisor/include/TimeRecord.h b/ChronoVisor/include/TimeRecord.h
--- a/ChronoVisor/include/TimeRecord.h (revision 36d412b0663a9fdc0c31969c260f39a7e349f51e)
+++ b/ChronoVisor/include/TimeRecord.h (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -8,17 +8,22 @@
#include <string>
#include <iomanip>
#include <sstream>
-
-#define TIME_DB_UPDATE_INTERVAL 3
+#include <cassert>
+#include "ClocksourceManager.h"
/**
* Class of each record in TimeDB, which might not be the same as TimeInfo in message serde
*/
class TimeRecord {
public:
- TimeRecord() : timestamp_(getCurTimestamp()), drift_rate_(0), update_interval_(-1) {}
+ TimeRecord() : clocksource_(nullptr), drift_rate_(0), ref_interval_(-1) {}
- TimeRecord(long timestamp, double update_interval) : timestamp_(timestamp), update_interval_(update_interval) {}
+ explicit TimeRecord(Clocksource *clocksource) : drift_rate_(0), ref_interval_(-1) {
+ clocksource_ = clocksource;
+ }
+
+ TimeRecord(Clocksource *clocksource, uint64_t timestamp, double ref_interval)
+ : clocksource_(clocksource), timestamp_(timestamp), ref_interval_(ref_interval) {}
TimeRecord& operator=(const TimeRecord& other) {
if (this == &other)
@@ -30,16 +35,15 @@
return *this;
}
- bool operator==(const TimeRecord& other) {
+ bool operator==(const TimeRecord& other) const {
return (this->timestamp_ == other.timestamp_) && (this->drift_rate_ == other.drift_rate_);
}
-
- bool operator!=(const TimeRecord& other) {
+ bool operator!=(const TimeRecord& other) const {
return (this->timestamp_ != other.timestamp_) || (this->drift_rate_ != other.drift_rate_);
}
- std::string toString() {
+ std::string toString() const {
std::string str = "timestamp: ";
str.append(std::to_string(timestamp_));
str.append(", drift rate: ");
@@ -51,59 +55,58 @@
return str;
}
- long getTimestamp() { return timestamp_; }
- double getDriftRate() { return drift_rate_; }
- double getDriftRate(long last_timestamp) {
- if (drift_rate_ < 0)
- return calCurDriftRate(last_timestamp);
- else return drift_rate_;
- }
- double getDriftRate(long last_timestamp, double update_interval) {
- if (drift_rate_ < 0)
- return calCurDriftRate(last_timestamp, update_interval);
- else return drift_rate_;
- }
+ Clocksource *getClocksource() { return clocksource_; }
+ uint64_t getTimestamp() const { return timestamp_; }
+ double getDriftRate() const { return drift_rate_; }
- void setUpdateInterval(double update_interval) { update_interval_ = update_interval; }
+ void setClocksource(Clocksource *clocksource) { clocksource_ = clocksource; }
+ void setUpdateInterval(double update_interval) { ref_interval_ = update_interval; }
+ void setTimestamp(uint64_t timestamp) { timestamp_ = timestamp; }
+ void setDriftRate(double drift_rate) { drift_rate_ = drift_rate; }
/**
- * Use maintained latest timestamp as the key to fetch the latest record in TimeDB
- * @param last_timestamp key to the latest record in TimeDB
+ * Update timestamp and drift rate
+ * @param last_timestamp timestamp at last time
* @return the latest TimeRecord with timestamp and drift rate
*/
- TimeRecord getCurTimeRecord(long last_timestamp);
+ TimeRecord updateTimeRecord(uint64_t last_timestamp) {
+ timestamp_ = getCurTimestamp();
+ drift_rate_ = calDriftRate(last_timestamp);
+ return *this;
+ }
/**
- * Update timestamp every interval, no mutex is needed since only one thrd writes to it
+ * Update timestamp every interval
*/
void updateTimestamp() { timestamp_ = getCurTimestamp(); }
+ /**
+ * Calculate drift rate using parameter last_timestamp and stored ref_interval_
+ * @param last_timestamp
+ * @return
+ */
+ double calDriftRate(uint64_t last_timestamp) {
+ assert(ref_interval_ != -1);
+ uint64_t cur = getCurTimestamp();
+ return (double) (cur - last_timestamp) / ref_interval_ / 1000000000.0 - 1;
+ }
+
public:
- long timestamp_; ///< updated every interval, key to fetch
- double drift_rate_; ///< drift rate calculated as diff(timestamp1, timestamp2)/interval
- double update_interval_; ///< update interval in second
+ uint64_t timestamp_{}; ///< updated every interval, key to fetch
+ double drift_rate_{}; ///< drift rate calculated as diff(timestamp1, timestamp2)/interval
+ double ref_interval_{}; ///< update interval in second
private:
/**
* Get system timestamp
* @return current system timestamp
*/
- long getCurTimestamp();
+ uint64_t getCurTimestamp() {
+ assert(clocksource_ != nullptr);
+ return clocksource_->getTimestamp();
+ }
- /**
- * Use last_timestamp as the key to fetch lastest drift rate in TimeDB, interval_ will be used for calculation
- * @param last_timestamp key in TimeeeDB
- * @return lastest drift rate
- */
- double calCurDriftRate(long last_timestamp);
-
- /**
- * Use last_timestamp as the key to fetch lastest drift rate in TimeDB, use update_interval for calculation instead
- * @param last_timestamp key in TimeeeDB
- * @param update_interval custom update interval for drift rate calculation
- * @return
- */
- double calCurDriftRate(long last_timestamp, double update_interval);
+ Clocksource *clocksource_; ///< clocksource (clock_gettime, std::high_precision_clock::now(), or TSC)
};
#endif //CHRONOLOG_TIMERECORD_H
Index: ChronoVisor/include/ChronoVisorServer.h
===================================================================
diff --git a/ChronoVisor/include/ChronoVisorServer.h b/ChronoVisor/include/ChronoVisorServer.h
--- a/ChronoVisor/include/ChronoVisorServer.h (revision d2fdbd0c8d77801388a5d8bf7f6cff3aaa806526)
+++ b/ChronoVisor/include/ChronoVisorServer.h (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -6,11 +6,13 @@
#define CHRONOLOG_CHRONOVISOR_H
#include <unordered_map>
+#include <atomic>
#include <rocksdb/db.h>
#include <TCPServer.h>
#include "Socket.h"
#include "type.h"
#include "log.h"
+#include "TimeManager.h"
#include "TimeRecord.h"
#include "ClientRegistryRecord.h"
#include "SerDe.h"
@@ -22,7 +24,12 @@
ChronoVisorServer(int port, int update_interval);
explicit ChronoVisorServer(int port);
- ~ChronoVisorServer() { if (pSerDe) { delete pSerDe; pSerDe = nullptr; } }
+ ~ChronoVisorServer() override {
+ if (pSerDe) { delete pSerDe; pSerDe = nullptr; }
+ if (pTimeManager) { delete pTimeManager; pTimeManager = nullptr; }
+ }
+
+ void setClocksourceType(ClocksourceType clocksourceType) { pTimeManager->setClocksourceType(clocksourceType); }
/**
* Handler invoked when server recvs sth
@@ -83,23 +90,38 @@
///@}
/**
- * @name Thread functions
- */
- void startTimeManagerThread();
-
- bool started_ = false;
- bool stopped_ = false;
-
- std::thread* timeManagerThread_ = nullptr;
+ * @name Periodic TimeDB update thread functions
+ */
+ void startPeriodicTimeDBUpdateThread();
+
+ /**
+ * @name On-demand TimeDB update thread functions
+ */
+ void startOnDemandTimeDBUpdateThread();
+
+
+ bool periodicThrdStarted_ = false;
+ bool periodicThrdStopped_ = false;
+ std::thread* periodicTimeDBUpdateThread_ = nullptr;
+
+ bool onDemandThrdStarted_ = false;
+ bool onDemandThrdStopped_ = false;
+ std::thread* onDemandTimeDBUpdateThread_ = nullptr;
+
+ TimeManager *pTimeManager;
+ /**
+ * @name In-memory time info related variables
+ */
+ ///@{
+ double timeDBUpdateInterval_; ///< in second
+ ///@}
/**
* @name TimeDB related variables
*/
///@{
- rocksdb::DB *timeDB_;
+ rocksdb::DB *timeDB_{};
std::mutex timeDBMutex_;
- long lastTimestamp_ = -1; ///< used as the key to get the lastest record in TimeDB
- double timeDBUpdateInterval_; ///< in second
const std::string timeDBName_ = "/tmp/chronolog_timedb";
///@}
@@ -122,8 +144,8 @@
/**
* @name Serialization/deserialization related variables
*/
- SerDeFactory *pSerDeFactory;
- SerDe *pSerDe;
+ SerDeFactory *pSerDeFactory{};
+ SerDe *pSerDe{};
};
}
Index: ChronoVisor/src/TimeManager.cpp
===================================================================
diff --git a/ChronoVisor/src/TimeManager.cpp b/ChronoVisor/src/TimeManager.cpp
new file mode 100644
--- /dev/null (revision 32d09797565725c7ee8a78d89831fb1878e14302)
+++ b/ChronoVisor/src/TimeManager.cpp (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -0,0 +1,5 @@
+//
+// Created by kfeng on 2/14/22.
+//
+
+#include "TimeManager.h"
Index: ChronoVisor/src/TimeManager.h
===================================================================
diff --git a/ChronoVisor/src/TimeManager.h b/ChronoVisor/src/TimeManager.h
new file mode 100644
--- /dev/null (revision 32d09797565725c7ee8a78d89831fb1878e14302)
+++ b/ChronoVisor/src/TimeManager.h (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -0,0 +1,88 @@
+//
+// Created by kfeng on 2/14/22.
+//
+
+#ifndef CHRONOLOG_TIMEMANAGER_H
+#define CHRONOLOG_TIMEMANAGER_H
+
+#include "ClocksourceManager.h"
+#include "TimeRecord.h"
+
+#define TIME_DB_INIT_WAIT 1
+#define TIME_DB_UPDATE_INTERVAL 3
+
+class TimeManager {
+public:
+ TimeManager() {
+ refTimestampUpdateInterval_ = TIME_DB_UPDATE_INTERVAL;
+ init(refTimestampUpdateInterval_);
+ }
+
+ explicit TimeManager(double updateInterval) {
+ refTimestampUpdateInterval_ = updateInterval;
+ init(refTimestampUpdateInterval_);
+ }
+
+ explicit TimeManager(double updateInterval, ClocksourceType clocksourceType) {
+ refTimestampUpdateInterval_ = updateInterval;
+ init(refTimestampUpdateInterval_, clocksourceType);
+ }
+
+ void setClocksourceType(ClocksourceType clocksourceType) { clocksourceType_ = clocksourceType; }
+
+ std::unique_ptr<TimeRecord> genPeriodicTimeRecord() {
+ std::unique_ptr<TimeRecord> record = std::make_unique<TimeRecord>(clocksource_);
+ if (firstTimeRecord_) {
+ // very first time of generating a TimeRecord for big bang, sleep 1s and calculate drift rate
+ record->updateTimestamp();
+ uint64_t lastTimestamp = record->getTimestamp();
+ record->setUpdateInterval(TIME_DB_INIT_WAIT);
+ usleep(TIME_DB_INIT_WAIT * 1e6);
+ record->updateTimeRecord(lastTimestamp);
+ latestDriftRate_ = record->getDriftRate();
+ } else {
+ record->setUpdateInterval(refTimestampUpdateInterval_);
+ record->setDriftRate(latestDriftRate_);
+ if (lastTimestamp_ == 0) {
+ // first interval TimeRecord
+ lastTimestamp_ = clocksource_->getTimestamp();
+ } else {
+ uint64_t curTimestamp_ = clocksource_->getTimestamp();
+ latestDriftRate_ = (double) (curTimestamp_ - lastTimestamp_) /
+ refTimestampUpdateInterval_ / 1000000000.0 - 1;
+ lastTimestamp_ = curTimestamp_;
+ }
+ }
+ firstTimeRecord_ = false;
+ record->updateTimestamp();
+ return record;
+ }
+
+ std::unique_ptr<TimeRecord> genOnDemandTimeRecord() {
+ std::unique_ptr<TimeRecord> record = std::make_unique<TimeRecord>(clocksource_);
+ record->updateTimestamp();
+ record->setDriftRate(latestDriftRate_);
+ return record;
+ }
+
+public:
+ Clocksource *clocksource_{}; ///< clocksource (clock_gettime, std::high_precision_clock::now(), or TSC)
+ double refTimestampUpdateInterval_; ///< how often reference timestamp is updated for drift rate calculation
+ uint64_t lastTimestamp_{};
+ std::atomic<double> latestDriftRate_{};
+ bool firstTimeRecord_{};
+ ClocksourceType clocksourceType_{};
+
+private:
+ void init(double updateInterval, ClocksourceType clocksourceType = ClocksourceType::C_STYLE) {
+ firstTimeRecord_ = true;
+ refTimestampUpdateInterval_ = updateInterval;
+ clocksourceType_ = clocksourceType;
+ ClocksourceManager *manager = ClocksourceManager::getInstance();
+ manager->setClocksourceType(clocksourceType_);
+ clocksource_ = manager->getClocksource();
+ }
+};
+
+
+#endif //CHRONOLOG_TIMEMANAGER_H
Index: test/integration/full_stack/chronolog_client.cpp
===================================================================
diff --git a/test/integration/full_stack/chronolog_client.cpp b/test/integration/full_stack/chronolog_client.cpp
--- a/test/integration/full_stack/chronolog_client.cpp (revision d2fdbd0c8d77801388a5d8bf7f6cff3aaa806526)
+++ b/test/integration/full_stack/chronolog_client.cpp (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -42,8 +42,9 @@
LOGD("server message received: %s", serverMsg->toString().c_str());
std::cout << "Latency: " << duration.count() * 1000 << " ms" << std::endl;
+ usleep(3e6);
ClientMessage clientMsg;
- clientMsg.msgType_ = ClientMessage::GETCHRONOKEEPERINFO;
+ clientMsg.msgType_ = ClientMessage::GETTIMEINFO;
std::unique_ptr<std::ostringstream> oss;
{
oss = pSerDe->serializeClientMessage(clientMsg);
Index: ChronoVisor/include/common.h
===================================================================
diff --git a/ChronoVisor/include/common.h b/ChronoVisor/include/common.h
new file mode 100644
--- /dev/null (revision 32d09797565725c7ee8a78d89831fb1878e14302)
+++ b/ChronoVisor/include/common.h (revision 32d09797565725c7ee8a78d89831fb1878e14302)
@@ -0,0 +1,19 @@
+//
+// Created by kfeng on 2/14/22.
+//
+
+#ifndef CHRONOLOG_COMMON_H
+#define CHRONOLOG_COMMON_H
+
+#include <queue>
+#include <mutex>
+#include "TimeRecord.h"
+#include "ClocksourceManager.h"
+
+ClocksourceManager *ClocksourceManager::clocksourceManager_ = nullptr;
+std::queue<TimeRecord> timeDBWriteQueue_;
+int timeDBWriteQueueSize_{};
+std::mutex timeDBWriteQueueMutex_;
+std::condition_variable timeDBWriteQueueCV_;
+
+#endif //CHRONOLOG_COMMON_H
Index: tools/Serde/include/MessageSerDe.h
===================================================================
diff --git a/tools/Serde/include/MessageSerDe.h b/tools/Serde/include/MessageSerDe.h
--- a/tools/Serde/include/MessageSerDe.h (revision 32d09797565725c7ee8a78d89831fb1878e14302)
+++ b/tools/Serde/include/MessageSerDe.h (revision 071e8a9e24686f44ce43289d92f170468923d6e6)
@@ -60,6 +60,10 @@
}
};
+const int CLIENTMSGTYPE_ADMIN_MASK = 1 << 4;
+const int CLIENTMSGTYPE_CHRONICLE_MASK = 1 << 8;
+const int CLIENTMSGTYPE_EVENT_MASK = 1 << 12;
+
/**
* \struct ClientMessage
* Structure to store all the info client sends to server
@@ -67,10 +71,18 @@
struct ClientMessage {
enum ClientMessageType {
UNKNOWN = -1,
- CONNECTION = 0,
- DISCONNECTION = 1,
- GETTIMEINFO = 2,
- GETCHRONOKEEPERINFO = 3
+ CONNECTION = CLIENTMSGTYPE_ADMIN_MASK + 1,
+ DISCONNECTION = CLIENTMSGTYPE_ADMIN_MASK + 2,
+ GETTIMEINFO = CLIENTMSGTYPE_ADMIN_MASK + 3,
+ GETCHRONOKEEPERINFO = CLIENTMSGTYPE_ADMIN_MASK + 4,
+ CREATECHRONICLE = CLIENTMSGTYPE_CHRONICLE_MASK + 1,
+ EDITCHRONICLE = CLIENTMSGTYPE_CHRONICLE_MASK + 2,
+ DESTROYCHRONICLE = CLIENTMSGTYPE_CHRONICLE_MASK + 3,
+ ACQUIRECHRONICLE = CLIENTMSGTYPE_CHRONICLE_MASK + 4,
+ RELEASECHRONICLE = CLIENTMSGTYPE_CHRONICLE_MASK + 5,
+ RECORDEVENT = CLIENTMSGTYPE_EVENT_MASK + 1,
+ PLAYBACKEVENT = CLIENTMSGTYPE_EVENT_MASK + 2,
+ REPLAYEVENT = CLIENTMSGTYPE_EVENT_MASK + 3
};
ClientMessageType msgType_; ///< type
@@ -92,6 +104,10 @@
}
};
+const int SERVERMSGTYPE_ADMIN_MASK = 1 << 4;
+const int SERVERMSGTYPE_CHRONICLE_MASK = 1 << 8;
+const int SERVERMSGTYPE_EVENT_MASK = 1 << 12;
+
/**
* \struct ServerMessage
* Structure to store all the info server sends to client
@@ -99,10 +115,15 @@
struct ServerMessage {
enum ServerMessageType {
UNKNOWN = -1,
- CONNRESPONSE = 0,
- DISCONNRESPONSE = 1,
- GETTIMEINFORESPONSE = 2,
- GETCHRONOKEEPERINFORESPONSE = 3
+ CONNRESPONSE = SERVERMSGTYPE_ADMIN_MASK + 1,
+ DISCONNRESPONSE = SERVERMSGTYPE_ADMIN_MASK + 2,
+ GETTIMEINFORESPONSE = SERVERMSGTYPE_ADMIN_MASK + 3,
+ GETCHRONOKEEPERINFORESPONSE = SERVERMSGTYPE_ADMIN_MASK + 4,
+ CREATECHRONICLERESPONSE = SERVERMSGTYPE_CHRONICLE_MASK + 1,
+ EDITCHRONICLERESPONSE = SERVERMSGTYPE_CHRONICLE_MASK + 2,
+ DESTROYCHRONICLERESPONSE = SERVERMSGTYPE_CHRONICLE_MASK + 3,
+ ACQUIRECHRONICLERESPONSE = SERVERMSGTYPE_CHRONICLE_MASK + 4,
+ RELEASECHRONICLERESPONSE = SERVERMSGTYPE_CHRONICLE_MASK + 5
};
ServerMessageType msgType_; ///< type