-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyser.py
1059 lines (887 loc) · 28.6 KB
/
analyser.py
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#*********************************************************************************************#
#*********************************************************************************************#
#******************witten by Neil Hao([email protected]), 2013/05/19*************************#
#*********************************************************************************************#
#*********************************************************************************************#
#*********************************************************************************************#
from datetime import timedelta
import os
import sys
#from cStringIO import StringIO
from StringIO import StringIO
from xml.dom import minidom
import logging
from base import *
from sampler import *
from anlyhelper import *
time_offset = False
class AnalyConfig( BaseObject ):
def __init__( self ):
self.type = ''
self.startTime = 0
self.endTime = -1
self.pace = 0
self.insertValue = None
self.outPath = ''
def __str__( self ):
return str( self.__dict__ )
def exist( self, member ):
return member in self.__dict__
class Analyser( BaseObject ):
def __init__( self, config, toFile = True ):
self.parser = None #this may be assigned outside
self.oneProcessMode = 1
self.atype = ''
self.startTime = config.startTime
self.endTime = config.endTime
self.startBufTime = 0
self.pace = config.pace
self.bufTime = BUF_TIME
self.sampler = None
self.tid = -1
self.fileStartTime = -1
self.fileEndTime = -1
self.firstFlush = True
if toFile:
self.outPath = config.outPath
self.errPath = config.outPath + '.errlog'
self.fout = None
self.ferr = None
self.filter = config.filter
def set_one_process_mode( self, mode ):
self.oneProcessMode = mode
def close_output_files( self ):
if self.fout is not None:
logging.info( 'close file:'+str(self.fout) )
self.fout.close()
self.fout = None
if self.ferr is not None:
logging.info( 'close err file:'+str(self.ferr) )
self.ferr.close()
self.ferr = None
def open_output_files( self ):
self.close_output_files()
self.fout = open( self.outPath, 'w' )
self.errPath = self.outPath + '.errlog'
#self.ferr = open( self.errPath, 'w' )
self.ferr = None
logging.info( 'open file:'+str(self.fout) )
def restart( self, startTime, endTime, bufTime ):
self.startTime = startTime
self.endTime = endTime
self.bufTime = bufTime
if self.sampler is not None:
args = self.sampler.args
args.startTime = startTime
args.endTime = endTime
self.sampler.reset( args )
def align_time_to_pace( self, otime ):
if otime > 0 and self.pace > 0:
otime = int(otime) / self.pace * self.pace
return otime
def get_sample_start_time( self, logInfo ):
returnTime = self.startTime
if logInfo is not None:
startTime = self.startTime
if self.sorted:
#for sorted logs, make sure we use the proper start time
if startTime <= 0 or startTime < logInfo.recvdTime:
returnTime = logInfo.recvdTime
else:
returnTime = startTime
else:
if startTime <= 0:
startTime = logInfo.recvdTime - BUF_TIME
startTime = self.align_time_to_pace( startTime )
returnTime = startTime
elif self.startTime > 0:
startBufTime = int(self.startBufTime)
if self.pace > 0:
startBufTime = startBufTime / self.pace * self.pace
self.startBufTime = startBufTime
self.startTime -= startBufTime
returnTime = self.startTime
logging.info( 'sampler startTime:'+str_seconds(returnTime) + ',tid:'+ str(self.tid) )
return returnTime
def get_sample_end_time( self, logInfo ):
endTime = self.align_time_to_pace( self.endTime )
logging.info( 'sampler endTime:'+str_seconds(endTime)+',tid:'+str(self.tid) )
return endTime
def analyse_log( self, logInfo ):
#control the startTime
if self.startTime > 0:
etime = logInfo.recvdTime + logInfo.servTime / 1000000
if etime < self.startTime:
return False
#control the endTime
if self.endTime > 0:
if logInfo.recvdTime > self.endTime:
return False
if self.filter is not None:
if not self.filter.filter(logInfo):
return False
ret = False
if self.pace == 0:
ret = self.anly_zero_pace( logInfo )
elif self.pace < 0:
ret = self.anly_negative_pace( logInfo )
else:
ret = self.anly_pace( logInfo )
if not ret and self.sampler is not None and self.sampler.startTime > self.startTime:
if self.ferr is None:
self.ferr = open( self.errPath, 'w' )
self.ferr.write( logInfo.originLine + '\n' )
def anly_zero_pace( self, logInfo ):
raise_virtual( func_name() )
def anly_negative_pace( self, logInfo ):
raise_virtual( func_name() )
def anly_pace( self, logInfo ):
raise_virtual( func_name() )
def decode_output_head( self, hstr ):
return True
def encode_output_head( self ):
return 'time;' + self.atype
def decode_output_value( self, vstr ):
raise_virtual( func_name() )
def anly_outut_value( self, valList ):
raise_virtual( func_name() )
def prepare_output( self ):
pass
def output_head( self ):
if self.fout is not None and self.firstFlush:
#someone may need do some init work before output
self.prepare_output()
head = self.encode_output_head()
if head is not None:
self.fout.write( head+'\n' )
self.firstFlush = False
def output_data( self, data, size ):
self.output_head()
if self.fout is not None:
self.output_head()
self.fout.write( data )
def flush( self ):
if self.sampler is not None:
self.sampler.flush()
def close( self ):
self.on_close()
if self.ferr is not None:
self.ferr.close()
self.ferr = None
if self.fout is not None:
self.fout.close()
self.fout = None
def on_close( self ):
raise_virtual( func_name() )
def exist( self, member ):
return member in self.__dict__
def __str__( self ):
ss = 'anly type:' + self.atype + ', output file:' +self.outPath+',pace:'+str(self.pace)
ss += ',startTime:' + str_seconds(self.startTime) + ',endTime:'+str_seconds(self.endTime)
return ss
class BandwidthAnalyser( Analyser ):
def __init__( self, config ):
super( BandwidthAnalyser, self ).__init__( config )
self.totalSent = 0
self.sampler = None
self.hasWritten = False
self.zeroValueCount = 0
self.zeroStartTime = 0
self.hasNoneZero = False
def anly_zero_pace( self, logInfo ):
servTime = logInfo.servTime / 1000000.0 #to second
sampleTime = logInfo.recvdTime
totalSent = logInfo.bytesSentAll
band = totalSent * 8.0 / servTime / 1024 / 1024
band = round( band, 3 )
dtime = to_datetime( sampleTime )
log = str_time( dtime ) + '\t' + str( band ) + '\t' + str(servTime) + '\t' + str(totalSent) + '\n'
self.fout.write( log )
return True
def anly_negative_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
servTime = logInfo.servTime / 1000000
if servTime == 0:
servTime = 1
value = logInfo.bytesSentAll
if self.sampler.add_sample( logInfo.recvdTime, value ) != 0:
return False
return True
def anly_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
servTime = logInfo.servTime / 1000000.0
if servTime == 0:
servTime = 1
value = logInfo.bytesSentAll
logTime = logInfo.recvdTime
startTime = self.sampler.startTime
pace = self.sampler.pace
valtul = time_average( value, logTime, servTime, startTime, pace )
(hval, (mstime, mval, midNum), (metime, endVal)) = valtul
ret = self.sampler.add_sample( logTime, hval)
if mstime > 0:
ret = self.sampler.add_samples( mstime, mval, midNum)
if ret > 0: #need to flash the buffer to the file
ctime = mstime
num = midNum
while ret > 0:
ctime += ret * self.sampler.pace
num -= ret
ret = self.sampler.add_samples( ctime, value, num )
if metime > 0:
ret = self.sampler.add_sample( metime, endVal)
if ret < 0:
return False
return True
def anly_pace_old( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
servTime = logInfo.servTime / 1000000.0
if servTime == 0:
servTime = 1
num = servTime / self.sampler.pace
if num <= 1:
value = logInfo.bytesSentAll
else:
value = logInfo.bytesSentAll / float(num)
num = int( num+0.9999 )
ret = self.sampler.add_samples( logInfo.recvdTime, value, num )
if ret < 0:
if self.hasWritten:
logging.debug( 'old log:'+str(logInfo) )
return False #TODO
elif ret > 0: #need to flash the buffer to the file
ctime = logInfo.recvdTime
while ret > 0:
logging.debug( '%%%%%%%%%%%%%%%%%%%'+str(ret) )
ctime += ret * self.sampler.pace
num -= ret
ret = self.sampler.add_samples( ctime, value, num )
if ret < 0:
return False
return True
def anly_outut_value( self, vtime, value ):
if self.sampler is None:
startTime = self.get_sample_start_time( None )
endTime = self.get_sample_end_time( None )
if startTime <= 0:
startTime = vtime
sargs = SamplerArgs( startTime, endTime, self.pace,
self.bufTime, NUM_THRES, BandwidthAnalyser.__flush_callback, self )
self.sampler = Sampler( sargs )
ret = self.sampler.add_sample( vtime, value )
return ret
def __create_sampler( self, logInfo ):
startTime = self.get_sample_start_time( logInfo )
endTime = self.get_sample_end_time( logInfo )
sargs = SamplerArgs( startTime, endTime, self.pace,
BUF_TIME, NUM_THRES, BandwidthAnalyser.__flush_callback, self )
self.sampler = Sampler( sargs )
def __flush_callback( self, sampler, blist ):
curTime = sampler.startTime
toAdd = 0
pace = sampler.pace
if pace > 0:
toAdd = pace / 2
if not time_offset:
toAdd = 0
bufio = StringIO()
minTime = self.align_time_to_pace( sampler.minTime )
maxTime = sampler.maxTime
if pace < 0:
minTime = maxTime = -1
logging.info( 'flush data,'+str(self) )
self.output_head()
for value in blist:
if curTime < minTime:
if value != 0:
logging.warn( '************************wrong filtering'+\
'curTime:'+str_seconds(curTime)+',minTime:'+str_seconds(minTime) )
curTime += pace
continue
if maxTime > 0 and curTime > maxTime:
break
self.__write_line( bufio, value, curTime, toAdd, pace )
curTime += pace
ostr = bufio.getvalue()
self.fout.write( ostr )
self.hasWritten = True
def __write_line( self, bufio, value, curTime, toAdd, pace ):
dtime = to_datetime( curTime + toAdd )
tstr = str_time( dtime )
band = round( value * 8 / float(pace) / 1024 / 1024, 3 )
bufio.write( tstr )
bufio.write( ';' )
bufio.write( str(band) )
bufio.write( '\n' )
def decode_output_value( self, vstr ):
segs = vstr.split( ';' )
seconds = seconds_str( segs[0] )
pace = self.pace
if pace < 0:
pace = 1
band = float(segs[1]) * 1024 * 1024 / 8 * pace
return (seconds, band)
def on_close( self ):
logging.info( 'close'+str(self.__class__)+str(self) )
if self.sampler is not None:
self.sampler.flush()
self.sampler = None
class StatusAnalyser( Analyser ):
def __init__( self, config ):
super( StatusAnalyser, self ).__init__( config )
self.sampler = None
def anly_zero_pace( self, logInfo ):
tstr = str_seconds( logInfo.recvdTime )
log = tstr + ',' + str(logInfo) + '\n'
self.fout.write( log )
return True
def anly_negative_pace( self, logInfo ):
return self.anly_pace( logInfo )
def anly_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
value = dict()
value[logInfo.status] = 1
servTime = logInfo.servTime / 1000000 + 1
ret = self.sampler.add_sample( logInfo.recvdTime + servTime, value )
if ret != 0:
return False
return True
def anly_outut_value( self, vtime, value ):
if self.sampler is None:
startTime = self.get_sample_start_time( None )
endTime = self.get_sample_end_time( None )
if startTime <= 0:
startTime = int(vtime)
sargs = SamplerArgs( startTime, endTime, self.pace,
self.bufTime, NUM_THRES, StatusAnalyser.__flush_callback, self )
self.sampler = MutableSampler( sargs, StatusAnalyser.__init_value, StatusAnalyser.__update_value )
ret = self.sampler.add_sample( vtime, value )
return ret
def __create_sampler( self, logInfo ):
startTime = self.get_sample_start_time( logInfo )
endTime = self.get_sample_end_time( logInfo )
sargs = SamplerArgs( startTime, endTime, self.pace,
BUF_TIME, NUM_THRES, StatusAnalyser.__flush_callback, self )
self.sampler = MutableSampler( sargs, StatusAnalyser.__init_value, StatusAnalyser.__update_value )
def __init_value( self, value ):
return dict()
def __update_value( self, oldValue, sampleValue ):
for status in sampleValue.keys():
count = sampleValue[ status ]
if status in oldValue:
count += oldValue[ status ]
oldValue[status] = count
return oldValue
def __flush_callback( self, sampler, blist ):
curTime = sampler.startTime
toAdd = 0
pace = sampler.pace
if sampler.pace > 0:
toAdd = sampler.pace / 2
if not time_offset:
toAdd = 0
bufio = StringIO()
logging.info( 'flush data,'+str(self) )
minTime = self.align_time_to_pace( sampler.minTime )
maxTime = sampler.maxTime
if pace < 0:
minTime = maxTime = -1
self.output_head()
for item in blist:
if curTime < minTime:
curTime += pace
continue
if maxTime > 0 and curTime > maxTime:
break
if len(item) != 0:
sampleTime = curTime + toAdd
tstr = str_seconds( sampleTime )
bufio.write( tstr )
bufio.write( ';' )
bufio.write( str(item) )
bufio.write( '\n' )
curTime += pace
logs = bufio.getvalue()
self.fout.write( logs )
def decode_output_value( self, vstr ):
segs = vstr.split( ';' )
seconds = seconds_str( segs[0] )
pace = self.pace
if pace < 0:
pace = 1
value = segs[1]
#decode the map value
vmap = dict()
value = value[1:len(value)-1]
segs = value.split( ',' )
for seg in segs:
isegs = seg.split( ':' )
status = int( isegs[0].strip() )
count = int( isegs[1].strip() )
vmap[status] = count
return (seconds, vmap)
def on_close( self ):
logging.info( 'close'+str(self.__class__)+str(self) )
if self.sampler is not None:
self.sampler.flush()
self.sampler = None
class XactRateAnalyser( Analyser ):
def __init__( self, config ):
super( XactRateAnalyser, self ).__init__( config )
self.sampler = None
self.zeroValueCount = 0
self.zeroStartTime = 0
self.hasNoneZero = False
def anly_zero_pace( self, logInfo ):
tstr = str_seconds( logInfo.recvdTime )
log = tstr + ',' + str(logInfo) + '\n'
self.fout.write( log )
return True
def anly_negative_pace( self, logInfo ):
return self.anly_pace( logInfo )
if self.sampler is None:
self.__create_sampler( logInfo )
if self.sampler.add_sample( self.sampler.startTime, 1 ) != 0:
return False
return True
def anly_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
ret = self.sampler.add_sample( logInfo.recvdTime, 1 )
if ret != 0:
return False
return True
def anly_outut_value( self, vtime, value ):
if self.sampler is None:
startTime = self.get_sample_start_time( None )
endTime = self.get_sample_end_time( None )
if startTime <= 0:
startTime = int(vtime)
sargs = SamplerArgs( startTime, endTime, self.pace,
self.bufTime, NUM_THRES, XactRateAnalyser.__flush_callback, self )
self.sampler = Sampler( sargs )
ret = self.sampler.add_sample( vtime, value )
return ret
def __create_sampler( self, logInfo ):
startTime = self.get_sample_start_time( logInfo)
endTime = self.get_sample_end_time( logInfo )
sargs = SamplerArgs( startTime, endTime, self.pace,
BUF_TIME, NUM_THRES, XactRateAnalyser.__flush_callback, self )
self.sampler = Sampler( sargs )
def __flush_callback( self, sampler, blist ):
curTime = sampler.startTime
toAdd = 0
pace = sampler.pace
if sampler.pace > 0:
toAdd = sampler.pace / 2
if not time_offset:
toAdd = 0
bufio = StringIO()
logging.info( 'flush data,'+str(self) )
minTime = self.align_time_to_pace( sampler.minTime )
maxTime = sampler.maxTime
if pace < 0:
minTime = maxTime = -1
self.output_head()
for value in blist:
if curTime < minTime:
curTime += pace
continue
if maxTime > 0 and curTime > maxTime:
break
self.__write_line( bufio, value, curTime, toAdd, pace )
curTime += pace
logs = bufio.getvalue()
self.fout.write( logs )
def __write_line( self, bufio, value, time, toAdd, pace ):
sampleTime = time + toAdd
tstr = str_seconds( sampleTime )
bufio.write( tstr )
bufio.write( ';' )
if pace > 0:
value /= float(pace)
value = int( value + 0.5 )
bufio.write( str(value) )
bufio.write( '\n' )
def decode_output_value( self, vstr ):
segs = vstr.split( ';' )
seconds = seconds_str( segs[0] )
pace = self.pace
if pace < 0:
pace = 1
value = float( segs[1] ) * pace
return (seconds, value)
def on_close( self ):
logging.info( 'close'+str(self.__class__)+str(self) )
if self.sampler is not None:
self.sampler.flush()
self.sampler = None
class DescAnalyser( Analyser ):
def __init__( self, config ):
super( DescAnalyser, self ).__init__( config )
self.sampler = None
def anly_zero_pace( self, logInfo ):
tstr = str_seconds( logInfo.recvdTime )
log = tstr + ',' + str(logInfo) + '\n'
self.fout.write( log )
return True
def anly_negative_pace( self, logInfo ):
return self.anly_pace( logInfo )
def anly_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
if not logInfo.exist( 'requestDes' ):
logInfo.requestDes = 'null'
value = dict()
value[logInfo.requestDes] = 1
servTime = logInfo.servTime / 1000000 + 1
ret = self.sampler.add_sample( logInfo.recvdTime + servTime, value )
if ret != 0:
return False
return True
def anly_outut_value( self, vtime, value ):
if self.sampler is None:
startTime = self.get_sample_start_time( None )
endTime = self.get_sample_end_time( None )
if startTime <= 0:
startTime = int(vtime)
sargs = SamplerArgs( startTime, endTime, self.pace,
self.bufTime, NUM_THRES, DescAnalyser.__flush_callback, self )
self.sampler = MutableSampler( sargs, DescAnalyser.__init_value, DescAnalyser.__update_value )
ret = self.sampler.add_sample( vtime, value )
return ret
def __create_sampler( self, logInfo ):
startTime = self.get_sample_start_time( logInfo )
endTime = self.get_sample_end_time( logInfo )
sargs = SamplerArgs( startTime, endTime, self.pace,
BUF_TIME, NUM_THRES, DescAnalyser.__flush_callback, self )
self.sampler = MutableSampler( sargs, DescAnalyser.__init_value, DescAnalyser.__update_value )
def __init_value( self, value ):
return dict()
def __update_value( self, oldValue, sampleValue ):
for des in sampleValue.keys():
count = sampleValue[ des ]
if des in oldValue:
count += oldValue[ des ]
oldValue[des] = count
return oldValue
def __flush_callback( self, sampler, blist ):
curTime = sampler.startTime
toAdd = 0
pace = sampler.pace
if sampler.pace > 0:
toAdd = sampler.pace / 2
if not time_offset:
toAdd = 0
bufio = StringIO()
logging.info( 'flush data,'+str(self) )
minTime = self.align_time_to_pace( sampler.minTime )
maxTime = sampler.maxTime
if pace < 0:
minTime = maxTime = -1
self.output_head()
for item in blist:
if curTime < minTime:
curTime += pace
continue
if maxTime > 0 and curTime > maxTime:
break
if len(item) != 0:
sampleTime = curTime + toAdd
tstr = str_seconds( sampleTime )
bufio.write( tstr )
bufio.write( ';' )
bufio.write( str(item) )
bufio.write( '\n' )
curTime += pace
logs = bufio.getvalue()
self.fout.write( logs )
def decode_output_value( self, vstr ):
segs = vstr.split( ';' )
seconds = seconds_str( segs[0] )
pace = self.pace
if pace < 0:
pace = 1
value = segs[1]
return (seconds, value)
def on_close( self ):
logging.info( 'close'+str(self.__class__)+str(self) )
if self.sampler is not None:
self.sampler.flush()
self.sampler = None
class FilterAnalyser( Analyser ):
def __init__( self, config ):
super( FilterAnalyser, self ).__init__( config )
def anly_pace( self, logInfo ):
print logInfo.originLine
def on_close( self ):
pass
class SingleAnalyser( Analyser ):
def __init__( self, config, helper ):
super( SingleAnalyser, self ).__init__( config )
self.sampler = None
self.__helper = helper
self.firstFlush = True
def set_one_process_mode( self, mode ):
self.oneProcessMode = mode
self.__helper.set_one_process_mode( mode )
def anly_zero_pace( self, logInfo ):
tstr = str_seconds( logInfo.recvdTime )
log = tstr + ',' + str(logInfo) + '\n'
self.fout.write( log )
return True
def anly_negative_pace( self, logInfo ):
return self.anly_pace( logInfo )
def anly_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
if not logInfo.exist( 'requestDes' ):
logInfo.requestDes = 'null'
sampleTime = self.__helper.get_sample_time( logInfo )
value = self.__helper.get_value( logInfo )
if self.__helper.isSingleType:
ret = self.sampler.add_sample( sampleTime, value )
else:
sampleValList = value
for (avalue, num) in sampleValList:
if num < 2:
ret = self.sampler.add_sample( sampleTime, avalue )
else:
ret = self.sampler.add_samples( sampleTime, avalue, num )
if ret > 0: #need to flash the buffer to the file
ctime = sampleTime
logging.warn( '>>'+'******************need to try again*****************'+str(ret) )
while ret > 0:
ctime += ret * self.sampler.pace
num -= ret
ret = self.sampler.add_samples( ctime, avalue, num )
if ret != 0:
logging.debug( 'add sample failed, log time:'+str_seconds(logInfo.recvdTime)+\
str(sampleTime)+'ret:'+str(ret)+str(self.sampler)+str(self.__helper.isSingleType) )
return False
return True
def anly_outut_value( self, vtime, valList ):
if self.sampler is None:
startTime = self.get_sample_start_time( None )
endTime = self.get_sample_end_time( None )
if startTime <= 0:
startTime = int(vtime)
sargs = SamplerArgs( startTime, endTime, self.pace,
self.bufTime, self.__helper.sampleThres, SingleAnalyser.__flush_callback, self )
self.sampler = MutableSampler( sargs, SingleAnalyser.__init_value, SingleAnalyser.__update_value )
ret = 0
for value in valList:
ret = self.sampler.add_sample( vtime, value )
return ret
def __create_sampler( self, logInfo ):
startTime = self.get_sample_start_time( logInfo )
endTime = self.get_sample_end_time( logInfo )
bufTime = self.__helper.get_buf_time()
sargs = SamplerArgs( startTime, endTime, self.pace,
bufTime, self.__helper.sampleThres, SingleAnalyser.__flush_callback, self )
self.sampler = MutableSampler( sargs, SingleAnalyser.__init_value, SingleAnalyser.__update_value )
def __init_value( self, value ):
return self.__helper.init_value( value )
def __update_value( self, oldValue, sampleValue ):
return self.__helper.update_value( oldValue, sampleValue )
def __flush_callback( self, sampler, blist ):
curTime = sampler.startTime
toAdd = 0
pace = sampler.pace
if sampler.pace > 0:
toAdd = sampler.pace / 2
if not time_offset:
toAdd = 0
bufio = StringIO()
logging.info( 'flush data,'+str(self) )
split = self.__helper.get_split()
minTime = self.align_time_to_pace( sampler.minTime )
maxTime = sampler.maxTime
if pace < 0:
minTime = maxTime = -1
self.output_head()
lastTime = -1
for item in blist:
if curTime < minTime:
curTime += pace
continue
if maxTime > 0 and curTime > maxTime:
break
if not self.__helper.exclude_value( item ):
vstr = self.__helper.str_value( item )
if vstr is not None:
if split is not None and not self.__helper.useInterStr:
if self.fileStartTime < 0:
self.fileStartTime = curTime
sampleTime = curTime + toAdd
tstr = str_seconds( sampleTime )
bufio.write( tstr )
bufio.write( split )
logging.debug( tstr+vstr+'toAdd:'+str(toAdd)+'pace:'+str(pace) )
bufio.write( vstr )
bufio.write( '\n' )
if bufio.len > 10485760: #10Mbytes
logs = bufio.getvalue()
self.fout.write( logs )
bufio.close()
bufio = StringIO()
lastTime = curTime
curTime += pace
if split is not None and not self.__helper.useInterStr:
if lastTime > 0:
self.fileEndTime = lastTime
logs = bufio.getvalue()
self.fout.write( logs )
bufio.close()
def prepare_output( self ):
self.__helper.prepare_dump()
def decode_output_head( self, hstr ):
split = self.__helper.get_split()
offset = 0
if not self.__helper.useInterStr:
#must check if there is head
prefix = 'time' + split
if not hstr.startswith(prefix):
return True
#skip the 'time' part
offset = hstr.find( split ) + 1
else:
#the internal helper may use the parser to decoding
self.__helper.parser = self.parser
self.__helper.head_str( hstr, offset, split )
return True
def encode_output_head( self ):
vh = self.__helper.str_head()
if vh is None:
return None
split = self.__helper.get_split()
head = 'time' + split + vh
return head
def decode_output_value( self, vstr ):
split = self.__helper.get_split()
offset = 0
if not self.__helper.useInterStr:
offset = vstr.find( split )
tstr = vstr[ 0:offset ]
vtime = seconds_str( tstr )
offset += 1
(valList, offset) = self.__helper.value_str( vstr, offset, split )
return (vtime, valList)
self.__helper.parser = self.parser
(vtime, itemList, offset) = self.__helper.value_str( vstr, offset, split )
return (vtime, itemList)
def on_close( self ):
logging.info( 'close'+str(self.__class__)+str(self) )
if self.sampler is not None:
self.sampler.flush()
self.__helper.on_close()
self.sampler = None
class ActiveSessionsAnalyser( Analyser ):
def __init__( self, config ):
super( ActiveSessionsAnalyser, self ).__init__( config )
self.totalSent = 0
self.sampler = None
self.hasWritten = False
self.zeroValueCount = 0
self.zeroStartTime = 0
self.hasNoneZero = False
def anly_zero_pace( self, logInfo ):
return True
def anly_negative_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
if self.sampler.add_sample( logInfo.recvdTime, 1 ) != 0:
return False
return True
def anly_pace( self, logInfo ):
if self.sampler is None:
self.__create_sampler( logInfo )
servTime = logInfo.servTime / 1000000.0
if servTime == 0:
servTime = 1
num = servTime / self.sampler.pace
diff = num - int(num) #in case it's a integer
if diff > 0:
num = int( num+1 )
else:
num = int( num )
value = 1
ret = self.sampler.add_samples( logInfo.recvdTime, value, num )
if ret < 0:
if self.hasWritten:
logging.debug( 'old log'+str(logInfo) )
return False #TODO
elif ret > 0: #need to flash the buffer to the file
ctime = logInfo.recvdTime
while ret > 0:
ctime += ret * self.sampler.pace
num -= ret
ret = self.sampler.add_samples( ctime, value, num )
return True
def anly_outut_value( self, vtime, value ):
if self.sampler is None:
startTime = self.get_sample_start_time( None )
endTime = self.get_sample_end_time( None )
if startTime <= 0:
startTime = int(vtime)
sargs = SamplerArgs( startTime, endTime, self.pace,
self.bufTime, NUM_THRES, ActiveSessionsAnalyser.__flush_callback, self )
self.sampler = Sampler( sargs )
ret = self.sampler.add_sample( vtime, value )
return ret
def __create_sampler( self, logInfo ):
startTime = self.get_sample_start_time( logInfo )