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
|
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2009 NoMachine, http://www.nomachine.com/. */
/* */
/* NXCOMP, NX protocol compression and NX extensions to this software */
/* are copyright of NoMachine. Redistribution and use of the present */
/* software is allowed according to terms specified in the file LICENSE */
/* which comes in the source distribution. */
/* */
/* Check http://www.nomachine.com/licensing.html for applicability. */
/* */
/* NX and NoMachine are trademarks of Medialogic S.p.A. */
/* */
/* All rights reserved. */
/* */
/**************************************************************************/
#ifndef Statistics_H
#define Statistics_H
#include "NXproto.h"
#include "Misc.h"
#include "Timestamp.h"
class Proxy;
//
// Opcode 255 is for generic requests, 1 is for
// generic replies (those which haven't a speci-
// fic differential encoding), opcode 0 is for
// generic messages from the auxiliary channels.
//
#define STATISTICS_OPCODE_MAX 256
//
// Maximum length of the buffer allocated for
// the statistics output.
//
#define STATISTICS_LENGTH 16384
//
// Query type.
//
#define TOTAL_STATS 1
#define PARTIAL_STATS 2
#define NO_STATS 3
//
// Log level.
//
#define PANIC
#define WARNING
#undef TEST
#undef DEBUG
//
// Log the operations related to updating
// the control tokens.
//
#undef TOKEN
class Statistics
{
public:
//
// Use the proxy class to get access
// to the message stores.
//
Statistics(Proxy *proxy);
~Statistics();
void addIdleTime(unsigned int numMs)
{
transportPartial_.idleTime_ += numMs;
transportTotal_.idleTime_ += numMs;
}
void subIdleTime(unsigned int numMs)
{
transportPartial_.idleTime_ -= numMs;
transportTotal_.idleTime_ -= numMs;
}
void addReadTime(unsigned int numMs)
{
transportPartial_.readTime_ += numMs;
transportTotal_.readTime_ += numMs;
}
void subReadTime(unsigned int numMs)
{
transportPartial_.readTime_ -= numMs;
transportTotal_.readTime_ -= numMs;
}
void addWriteTime(unsigned int numMs)
{
transportPartial_.writeTime_ += numMs;
transportTotal_.writeTime_ += numMs;
}
void subWriteTime(unsigned int numMs)
{
transportPartial_.writeTime_ -= numMs;
transportTotal_.writeTime_ -= numMs;
}
void addBytesIn(unsigned int numBytes)
{
transportPartial_.proxyBytesIn_ += numBytes;
transportTotal_.proxyBytesIn_ += numBytes;
}
double getBytesIn()
{
return transportTotal_.proxyBytesIn_;
}
void addBytesOut(unsigned int numBytes)
{
transportPartial_.proxyBytesOut_ += numBytes;
transportTotal_.proxyBytesOut_ += numBytes;
}
double getBytesOut()
{
return transportTotal_.proxyBytesOut_;
}
void addFrameIn()
{
transportPartial_.proxyFramesIn_++;
transportTotal_.proxyFramesIn_++;
#ifdef TEST
*logofs << "Statistics: Updated total proxy frames in to "
<< transportTotal_.proxyFramesIn_ << " at "
<< strMsTimestamp() << ".\n" << logofs_flush;
#endif
}
void addFrameOut()
{
transportPartial_.proxyFramesOut_++;
transportTotal_.proxyFramesOut_++;
#ifdef TEST
*logofs << "Statistics: Updated total proxy frames out to "
<< transportTotal_.proxyFramesOut_ << " at "
<< strMsTimestamp() << ".\n"
<< logofs_flush;
#endif
}
void addWriteOut()
{
transportPartial_.proxyWritesOut_++;
transportTotal_.proxyWritesOut_++;
#ifdef TEST
*logofs << "Statistics: Updated total proxy writes out to "
<< transportTotal_.proxyWritesOut_ << " at "
<< strMsTimestamp() << ".\n" << logofs_flush;
#endif
}
void addCompressedBytes(unsigned int bytesIn, unsigned int bytesOut);
void addDecompressedBytes(unsigned int bytesIn, unsigned int bytesOut)
{
transportPartial_.decompressedBytesIn_ += bytesIn;
transportTotal_.decompressedBytesIn_ += bytesIn;
transportPartial_.decompressedBytesOut_ += bytesOut;
transportTotal_.decompressedBytesOut_ += bytesOut;
}
void addFramingBits(unsigned int bitsOut)
{
transportPartial_.framingBitsOut_ += bitsOut;
transportTotal_.framingBitsOut_ += bitsOut;
proxyData_.protocolCount_ += bitsOut;
}
void addCachedRequest(unsigned int opcode)
{
protocolPartial_.requestCached_[opcode]++;
protocolTotal_.requestCached_[opcode]++;
}
void addRenderCachedRequest(unsigned int opcode)
{
protocolPartial_.renderRequestCached_[opcode]++;
protocolTotal_.renderRequestCached_[opcode]++;
}
void addRepliedRequest(unsigned int opcode)
{
protocolPartial_.requestReplied_[opcode]++;
protocolTotal_.requestReplied_[opcode]++;
}
void addCachedReply(unsigned int opcode)
{
protocolPartial_.replyCached_[opcode]++;
protocolTotal_.replyCached_[opcode]++;
}
void addRequestBits(unsigned int opcode, unsigned int bitsIn, unsigned int bitsOut)
{
#ifdef DEBUG
*logofs << "Statistcs: Added " << bitsIn << " bits in and "
<< bitsOut << " bits out to opcode " << opcode
<< ".\n" << logofs_flush;
#endif
protocolPartial_.requestCount_[opcode]++;
protocolTotal_.requestCount_[opcode]++;
protocolPartial_.requestBitsIn_[opcode] += bitsIn;
protocolTotal_.requestBitsIn_[opcode] += bitsIn;
protocolPartial_.requestBitsOut_[opcode] += bitsOut;
protocolTotal_.requestBitsOut_[opcode] += bitsOut;
//
// Don't account the split bits
// to the control token.
//
if (opcode != X_NXSplitData && opcode != X_NXSplitEvent)
{
proxyData_.protocolCount_ += bitsOut;
}
}
void addRenderRequestBits(unsigned int opcode, unsigned int bitsIn, unsigned int bitsOut)
{
#ifdef DEBUG
*logofs << "Statistcs: Added " << bitsIn << " bits in and "
<< bitsOut << " bits out to render opcode " << opcode
<< ".\n" << logofs_flush;
#endif
protocolPartial_.renderRequestCount_[opcode]++;
protocolTotal_.renderRequestCount_[opcode]++;
protocolPartial_.renderRequestBitsIn_[opcode] += bitsIn;
protocolTotal_.renderRequestBitsIn_[opcode] += bitsIn;
protocolPartial_.renderRequestBitsOut_[opcode] += bitsOut;
protocolTotal_.renderRequestBitsOut_[opcode] += bitsOut;
}
void addReplyBits(unsigned int opcode, unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.replyCount_[opcode]++;
protocolTotal_.replyCount_[opcode]++;
protocolPartial_.replyBitsIn_[opcode] += bitsIn;
protocolTotal_.replyBitsIn_[opcode] += bitsIn;
protocolPartial_.replyBitsOut_[opcode] += bitsOut;
protocolTotal_.replyBitsOut_[opcode] += bitsOut;
proxyData_.protocolCount_ += bitsOut;
}
void addEventBits(unsigned int opcode, unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.eventCount_[opcode]++;
protocolTotal_.eventCount_[opcode]++;
protocolPartial_.eventBitsIn_[opcode] += bitsIn;
protocolTotal_.eventBitsIn_[opcode] += bitsIn;
protocolPartial_.eventBitsOut_[opcode] += bitsOut;
protocolTotal_.eventBitsOut_[opcode] += bitsOut;
proxyData_.protocolCount_ += bitsOut;
}
void addCupsBits(unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.cupsCount_++;
protocolTotal_.cupsCount_++;
protocolPartial_.cupsBitsIn_ += bitsIn;
protocolTotal_.cupsBitsIn_ += bitsIn;
protocolPartial_.cupsBitsOut_ += bitsOut;
protocolTotal_.cupsBitsOut_ += bitsOut;
}
void addSmbBits(unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.smbCount_++;
protocolTotal_.smbCount_++;
protocolPartial_.smbBitsIn_ += bitsIn;
protocolTotal_.smbBitsIn_ += bitsIn;
protocolPartial_.smbBitsOut_ += bitsOut;
protocolTotal_.smbBitsOut_ += bitsOut;
}
void addMediaBits(unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.mediaCount_++;
protocolTotal_.mediaCount_++;
protocolPartial_.mediaBitsIn_ += bitsIn;
protocolTotal_.mediaBitsIn_ += bitsIn;
protocolPartial_.mediaBitsOut_ += bitsOut;
protocolTotal_.mediaBitsOut_ += bitsOut;
}
void addHttpBits(unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.httpCount_++;
protocolTotal_.httpCount_++;
protocolPartial_.httpBitsIn_ += bitsIn;
protocolTotal_.httpBitsIn_ += bitsIn;
protocolPartial_.httpBitsOut_ += bitsOut;
protocolTotal_.httpBitsOut_ += bitsOut;
}
void addFontBits(unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.fontCount_++;
protocolTotal_.fontCount_++;
protocolPartial_.fontBitsIn_ += bitsIn;
protocolTotal_.fontBitsIn_ += bitsIn;
protocolPartial_.fontBitsOut_ += bitsOut;
protocolTotal_.fontBitsOut_ += bitsOut;
}
void addSlaveBits(unsigned int bitsIn, unsigned int bitsOut)
{
protocolPartial_.slaveCount_++;
protocolTotal_.slaveCount_++;
protocolPartial_.slaveBitsIn_ += bitsIn;
protocolTotal_.slaveBitsIn_ += bitsIn;
protocolPartial_.slaveBitsOut_ += bitsOut;
protocolTotal_.slaveBitsOut_ += bitsOut;
}
void addPackedBytesIn(unsigned int numBytes)
{
packedPartial_.packedBytesIn_ += numBytes;
packedTotal_.packedBytesIn_ += numBytes;
}
void addPackedBytesOut(unsigned int numBytes)
{
packedPartial_.packedBytesOut_ += numBytes;
packedTotal_.packedBytesOut_ += numBytes;
}
void addSplit()
{
splitPartial_.splitCount_++;
splitTotal_.splitCount_++;
}
void addSplitAborted()
{
splitPartial_.splitAborted_++;
splitTotal_.splitAborted_++;
}
void addSplitAbortedBytesOut(unsigned int numBytes)
{
splitPartial_.splitAbortedBytesOut_ += numBytes;
splitTotal_.splitAbortedBytesOut_ += numBytes;
}
//
// Add the recorded protocol data to the proxy
// token counters. We want to send bpth the token
// request message and the data payload using a
// single system write, so we must guess how many
// output bytes we will generate.
//
void updateControlToken(int &count)
{
//
// Total is the total number of protocol bytes
// generated so far. We have saved the number
// of bytes generated the last time the function
// was called so we can calculate the difference.
//
// The number of protocol bits is updated as soon
// as new bits are accumulated, to avoid summing
// up all the opcodes in this routine. We add a
// byte to the control bytes difference to account
// for the framing bits that are very likely to
// be added to the payload.
//
double total = proxyData_.protocolCount_ / 8;
double diff = total - proxyData_.controlCount_ + 1;
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! Protocol bytes are "
<< total << " control bytes are "
<< proxyData_.controlCount_ << " difference is "
<< diff << ".\n" << logofs_flush;
#endif
count += (int) (diff / proxyData_.streamRatio_);
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! Adding "
<< (int) (diff / proxyData_.streamRatio_)
<< " bytes to the control token with ratio "
<< proxyData_.streamRatio_ << ".\n"
<< logofs_flush;
#endif
proxyData_.controlCount_ = total;
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! New control token has "
<< count << " bytes with total control bytes "
<< proxyData_.controlCount_ << ".\n"
<< logofs_flush;
#endif
}
void updateSplitToken(int &count)
{
double total = (protocolTotal_.requestBitsOut_[X_NXSplitData] +
protocolTotal_.eventBitsOut_[X_NXSplitEvent]) / 8;
double diff = total - proxyData_.splitCount_;
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! Protocol bytes are "
<< total << " split bytes are "
<< proxyData_.splitCount_ << " difference is "
<< diff << ".\n" << logofs_flush;
#endif
count += (int) (diff / proxyData_.streamRatio_);
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! Adding "
<< (int) (diff / proxyData_.streamRatio_)
<< " bytes to the split token with ratio "
<< proxyData_.streamRatio_ << ".\n"
<< logofs_flush;
#endif
proxyData_.splitCount_ = total;
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! New split token has "
<< count << " bytes with total split bytes "
<< proxyData_.splitCount_ << ".\n"
<< logofs_flush;
#endif
}
void updateDataToken(int &count)
{
double total = (protocolTotal_.cupsBitsOut_ +
protocolTotal_.smbBitsOut_ +
protocolTotal_.mediaBitsOut_ +
protocolTotal_.httpBitsOut_ +
protocolTotal_.fontBitsOut_ +
protocolTotal_.slaveBitsOut_) / 8;
double diff = total - proxyData_.dataCount_;
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! Protocol bytes are "
<< total << " data bytes are "
<< proxyData_.dataCount_ << " difference is "
<< diff << ".\n" << logofs_flush;
#endif
count += (int) (diff / proxyData_.streamRatio_);
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! Adding "
<< (int) (diff / proxyData_.streamRatio_)
<< " bytes to the data token with ratio "
<< proxyData_.streamRatio_ << ".\n"
<< logofs_flush;
#endif
proxyData_.dataCount_ = total;
#if defined(TEST) || defined(TOKEN)
*logofs << "Statistics: TOKEN! New data token has "
<< count << " bytes with total data bytes "
<< proxyData_.dataCount_ << ".\n"
<< logofs_flush;
#endif
}
//
// Update the current bitrate.
//
void updateBitrate(int bytes);
//
// Return the current bitrate.
//
int getBitrateInShortFrame()
{
return bitrateInShortFrame_;
}
int getBitrateInLongFrame()
{
return bitrateInLongFrame_;
}
int getTopBitrate()
{
return topBitrate_;
}
void resetTopBitrate()
{
topBitrate_ = 0;
}
double getStreamRatio()
{
return proxyData_.streamRatio_;
}
//
// Manage the congestion level.
//
void updateCongestion(int remaining, int limit);
double getCongestionInFrame()
{
return congestionInFrame_;
}
//
// Produce a dump of the statistics on
// the provided buffer.
//
int getClientCacheStats(int type, char *&buffer);
int getClientProtocolStats(int type, char *&buffer);
int getClientOverallStats(int type, char *&buffer);
int getServerCacheStats(int type, char *&buffer);
int getServerProtocolStats(int type, char *&buffer);
int getServerOverallStats(int type, char *&buffer);
int resetPartialStats();
private:
int getTimeStats(int type, char *&buffer);
int getServicesStats(int type, char *&buffer);
int getFramingStats(int type, char *&buffer);
int getBitrateStats(int type, char *&buffer);
int getStreamStats(int type, char *&buffer);
int getSplitStats(int type, char *&buffer);
struct T_protocolData
{
double requestCached_[STATISTICS_OPCODE_MAX];
double requestReplied_[STATISTICS_OPCODE_MAX];
double requestCount_[STATISTICS_OPCODE_MAX];
double requestBitsIn_[STATISTICS_OPCODE_MAX];
double requestBitsOut_[STATISTICS_OPCODE_MAX];
double renderRequestCached_[STATISTICS_OPCODE_MAX];
double renderRequestCount_[STATISTICS_OPCODE_MAX];
double renderRequestBitsIn_[STATISTICS_OPCODE_MAX];
double renderRequestBitsOut_[STATISTICS_OPCODE_MAX];
double replyCached_[STATISTICS_OPCODE_MAX];
double replyCount_[STATISTICS_OPCODE_MAX];
double replyBitsIn_[STATISTICS_OPCODE_MAX];
double replyBitsOut_[STATISTICS_OPCODE_MAX];
double eventCached_[STATISTICS_OPCODE_MAX];
double eventCount_[STATISTICS_OPCODE_MAX];
double eventBitsIn_[STATISTICS_OPCODE_MAX];
double eventBitsOut_[STATISTICS_OPCODE_MAX];
double cupsCount_;
double cupsBitsIn_;
double cupsBitsOut_;
double smbCount_;
double smbBitsIn_;
double smbBitsOut_;
double mediaCount_;
double mediaBitsIn_;
double mediaBitsOut_;
double httpCount_;
double httpBitsIn_;
double httpBitsOut_;
double fontCount_;
double fontBitsIn_;
double fontBitsOut_;
double slaveCount_;
double slaveBitsIn_;
double slaveBitsOut_;
};
struct T_transportData
{
double idleTime_;
double readTime_;
double writeTime_;
double proxyBytesIn_;
double proxyBytesOut_;
double proxyFramesIn_;
double proxyFramesOut_;
double proxyWritesOut_;
double compressedBytesIn_;
double compressedBytesOut_;
double decompressedBytesIn_;
double decompressedBytesOut_;
double framingBitsOut_;
};
struct T_packedData
{
double packedBytesIn_;
double packedBytesOut_;
};
struct T_splitData
{
double splitCount_;
double splitAborted_;
double splitAbortedBytesOut_;
};
struct T_overallData
{
double overallBytesIn_;
double overallBytesOut_;
};
struct T_proxyData
{
double protocolCount_;
double controlCount_;
double splitCount_;
double dataCount_;
double streamRatio_;
};
T_protocolData protocolPartial_;
T_protocolData protocolTotal_;
T_transportData transportPartial_;
T_transportData transportTotal_;
T_packedData packedPartial_;
T_packedData packedTotal_;
T_splitData splitPartial_;
T_splitData splitTotal_;
T_overallData overallPartial_;
T_overallData overallTotal_;
T_proxyData proxyData_;
//
// Used to calculate the bandwidth usage
// of the proxy link.
//
T_timestamp startShortFrameTs_;
T_timestamp startLongFrameTs_;
T_timestamp startFrameTs_;
int bytesInShortFrame_;
int bytesInLongFrame_;
int bitrateInShortFrame_;
int bitrateInLongFrame_;
int topBitrate_;
double congestionInFrame_;
//
// Need the proxy pointer to print the
// statistics related to the client and
// server stores and to add the protocol
// data to the proxy token accumulators.
//
Proxy *proxy_;
};
#endif /* Statistics_H */
|