-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_cache.py
1583 lines (1396 loc) · 88.1 KB
/
sim_cache.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
import copy
import gc
import json
import os
import random
from collections import defaultdict
import traceback
import sys
import time
import pickle
import pprint
import compress_json
import numpy as np
import pandas as pd
from . import admission_policies as aps
from . import eviction_policies as evictp
from . import dynamic_features as dyn_features
from . import prefetchers
from . import utils
from .utils import LOG_IOPS, ods, fmt_dur
from ..episodic_analysis.episodes import service_time
from ..episodic_analysis.episodes import st_to_util
from .ep_helpers import _lookup_episode
from .ep_helpers import Timestamp
from .ep_helpers import record_service_time_get
from .ep_helpers import record_service_time_put
from .ep_helpers import AccessPlus
CACHE_LOCATIONS = {
"": "",
"_admitbuffer": "AdmitBuffer",
"_flash": "Flash",
"_ram_prefetch": "RamPrefetch",
"_ram_prefetch_firsthit": "RamPrefetchFirstHit",
"_flash_noram": "FlashNotInRam",
"_flash_prefetch": "FlashPrefetch",
"_flash_prefetch_firsthit": "FlashPrefetchFirstHit",
"_ram": "Ram",
}
class StatsDumper(object):
def __init__(self, cache, logjson, output_dir, filename, *,
trace_stats=None, ram_cache=None, prefetcher=None,
admission_policy=None,
start_time=None, skip_first_secs=None):
self.cache = cache
self.ram_cache = ram_cache
self.prefetcher = prefetcher
self.logjson = logjson
self.output_dir = output_dir
self.filename = filename
self.trace_stats = trace_stats
self.start_time = start_time
self.skip_first_secs = skip_first_secs
self.df_analysis = None
self.admission_policy = admission_policy
analysis_filename = os.path.join(output_dir, "df_analysis.csv")
if os.path.exists(analysis_filename):
self.df_analysis = pd.read_csv(analysis_filename)
def dump(self, stats_, *, suffix="", verbose=False, dump_stats=False):
logjson = self.logjson
trace_duration_secs = logjson["traceSeconds"]
sample_ratio = logjson["sampleRatio"]
total_iops_get = logjson["totalIOPSGet"]
chunk_queries = ods.get("chunk_queries")
prefetcher_enabled = self.prefetcher.enabled
# ods.span("puts_ios", i=bi)
# This should be the only use of ods.last()
secs_so_far = ods.last("time_phy")[1] - ods.get("start_ts_phy")
iops_requests_sofar = ods.get("iops_requests")
logjson["results"]["ServiceTimeTotalOrig"] = service_time(iops_requests_sofar, chunk_queries)
# TODO: if we skip the firstm, we also need to decrease duration_s here.
util_kwargs = dict(sample_ratio=logjson["sampleRatio"], duration_s=secs_so_far)
util_peak_kwargs = dict(sample_ratio=logjson["sampleRatio"], duration_s=logjson['options']['log_interval'])
# TODO: Deprecate.
for k, v in CACHE_LOCATIONS.items():
k_ = "/"+k[1:] if k.startswith("_") else k
logjson["results"][f"TotalIOPSSaved{v}"] = ods.get(f"iops_saved{k_}")
logjson["results"][f"TotalIOPSSaved{v}Only"] = ods.get(f"iops_saved{k_}_only")
logjson["results"][f"TotalIOPSPartialHits{v}"] = ods.get(f"iops_partial_hits{k_}")
logjson["results"][f"IOPSSaved{v}Ratio"] = utils.safe_div(logjson["results"][f"TotalIOPSSaved{v}"], iops_requests_sofar)
logjson["results"][f"IOPSSaved{v}OnlyRatio"] = utils.safe_div(logjson["results"][f"TotalIOPSSaved{v}Only"], iops_requests_sofar)
logjson["results"][f"IOPSPartialHits{v}Ratio"] = utils.safe_div(logjson["results"][f"TotalIOPSPartialHits{v}"], iops_requests_sofar)
logjson["results"][f"TotalChunkHits{v}"] = ods.get(f"chunk_hits{k_}")
logjson["results"][f"TotalChunkSaved{v}"] = ods.get(f"chunk_saved{k_}")
logjson["results"][f"ChunkHit{v}Ratio"] = utils.safe_div(logjson["results"][f"TotalChunkHits{v}"], chunk_queries)
flash_queries = chunk_queries
if "TotalChunkHitsRam" in logjson["results"]:
flash_queries -= logjson["results"]["TotalChunkHitsRam"]
logjson["results"]["TotalFlashQueries"] = flash_queries
logjson["results"]["FlashCacheHitRate"] = utils.safe_div(logjson["results"]["TotalChunkHitsFlashNotInRam"], flash_queries)
logjson["results"]["ClientBandwidth"] = utils.mb_per_sec(chunk_queries, secs_so_far, sample_ratio)
chunks_written = ods.get("flashcache/keys_written")
# logjson["results"]["ChunkWritten"] = stats["chunks_written"]
# writes_per_reqs = chunks_written / total_iops * sample_ratio
logjson["results"]["FlashWriteRate"] = utils.mb_per_sec(
chunks_written, secs_so_far, sample_ratio
)
logjson["results"]["FlashPrefetchWriteRate"] = utils.mb_per_sec(
ods.get("flashcache/prefetches"), secs_so_far, sample_ratio
)
logjson["results"]["TotalChunkWritten"] = chunks_written
logjson["results"]["FlashChunkWritten"] = chunks_written
caches = {'': [self.cache, "flashcache"]}
if self.ram_cache:
caches['RamCache'] = [self.ram_cache, "ramcache"]
logjson["results"]["RamWriteRate"] = utils.mb_per_sec(
ods.get("ramcache/keys_written"), secs_so_far, sample_ratio
)
logjson['results']['RamChunkWritten'] = ods.get("ramcache/keys_written")
logjson["results"]["RamPrefetchWriteRate"] = utils.mb_per_sec(
ods.get("ramcache/prefetches"), secs_so_far, sample_ratio
)
logjson["results"]["RamCacheHitRate"] = utils.safe_div(logjson["results"]["TotalChunkHitsRam"], ods.get("ramcache/queries"))
else:
logjson['results']['RamWriteRate'] = 0
logjson['results']['RamChunkWritten'] = 0
logjson['results']['RamPrefetchWriteRate'] = 0
for label, (cache, cache_ns) in caches.items():
# TODO: Replace with ODS stats.
logjson["results"][label + "AvgMaxInterarrivalTime"] = cache.computeAvgMaxInterarrivalTime()
logjson["results"][label + "MaxMaxInterarrivalTime"] = max(logjson["results"][label + "AvgMaxInterarrivalTime"], max(ods.get("max_ia_max", init=[Timestamp(0, 0)])))
logjson["results"][label + "WarmupFinished"] = ods.get(f"{cache_ns}/warmup_finished")
logjson["results"][label + "AvgMaxInterarrivalTimeEvicted"] = utils.safe_div(ods.get(f"{cache_ns}/max_interarrival_time_cum"), ods.get(f"{cache_ns}/evictions"))
logjson["results"][label + "AvgEvictionAge"] = utils.safe_div(ods.get(f"{cache_ns}/eviction_age_cum"), ods.get(f"{cache_ns}/evictions"))
logjson["results"][label + "AvgNoHitEvictionAge"] = utils.safe_div(ods.get(f"{cache_ns}/unaccessed_eviction_age_cum"), ods.get(f"{cache_ns}/unaccessed_evictions"))
logjson["results"][label + "KeysWritten"] = ods.get(f"{cache_ns}/keys_written")
logjson["results"][label + "NumCacheEviction"] = ods.get(f"{cache_ns}/evictions")
logjson["results"][label + "MeanTimeInSystem"] = utils.safe_div(ods.get(f"{cache_ns}/total_time_in_system"), ods.get(f"{cache_ns}/keys_written"))
logjson["results"][label + "NumNoHitEvictions"] = ods.get(f"{cache_ns}/unaccessed_evictions")
logjson["results"][label + "NumEarlyEvictions"] = ods.get(f"{cache_ns}/early_evictions")
logjson["results"][label + "NumPrefetches"] = ods.get(f"{cache_ns}/prefetches")
logjson["results"][label + "NumPrefetchesFirstAcc"] = ods.get(f"{cache_ns}/prefetches_failed_firstaccess")
# logjson["results"][label + "NumPrefetchesCaught"] = cache.prefetches_caught
logjson["results"][label + "NumFailedPrefetchesExists"] = ods.get(f"{cache_ns}/prefetches_failed_exists")
logjson["results"][label + "NumFailedPrefetches"] = logjson["results"][label + "NumPrefetchesFirstAcc"] + logjson["results"][label + "NumFailedPrefetchesExists"]
# logjson["results"][label + "NumEpisodeTouches"] = cache.episode_touches
logjson["results"][label + "NumCacheRejection"] = ods.get(f"{cache_ns}/rejections")
logjson["results"][label + "AcceptanceRatio"] = utils.safe_div(logjson["results"][label + "KeysWritten"], logjson["results"][label + "KeysWritten"] + logjson["results"][label + "NumCacheRejection"])
logjson["results"][label + "PrefetchSuccessRate"] = utils.safe_div(logjson["results"][label + "NumPrefetches"], logjson["results"][label + "NumPrefetches"] + logjson["results"][label + "NumPrefetchesFirstAcc"])
# == Pinning down mistakes ==
# = Comparing to analysis =
# Eviction Age
# Extra writes
logjson["results"]["AssumedFlashEaTooLong"] = ods.get('flashcache/evicted_with_hits_remaining')
# Bonus hits
logjson["results"]["AssumedFlashEaTooShort"] = ods.get('flashcache/evicted_without_hits_remaining_hitsAfterEp')
# -- This shouldn't exist? + ods.get('flashcache/evicted_with_hits_remaining_hitsAfterEp')
logjson["results"]["TooShortEaBonusServiceTimeSavedRatio"] = utils.safe_div(service_time(0, logjson["results"]["AssumedFlashEaTooShort"]), logjson["results"]["ServiceTimeTotalOrig"])
# TODO: Bonus IOPS Saved Ratio. Assume it saves one miss per episode that you join.
logjson["results"]["AnalysisServiceTimeSaved"] = ods.get("flashcache/service_time_saved__{}prefetch_from_episode".format("" if prefetcher_enabled else "no"))
logjson["results"]["AnalysisServiceTimeSavedRatio"] = utils.safe_div(logjson["results"]["AnalysisServiceTimeSaved"], logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["AnalysisServiceTimeSavedFromPrefetchRatio"] = utils.safe_div(ods.get("flashcache/service_time_saved_pf_from_episode"), logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["AnalysisIOPSSavedRatio"] = utils.safe_div(ods.get("flashcache/hits__prefetch_from_episode"), iops_requests_sofar)
logjson["results"]["AnalysisAdmittedChunkRatio"] = utils.safe_div(ods.get("flashcache/admitted_chunks_from_analysis"), chunks_written)
logjson["results"]["AnalysisAdmittedWriteRate"] = utils.mb_per_sec(
ods.get("flashcache/admitted_chunks_from_analysis"), secs_so_far, sample_ratio
)
logjson["results"]["AnalysisOfflineWR"] = -1
logjson["results"]["AnalysisOfflineServiceTimeSavedRatio"] = -1
logjson["results"]["AnalysisOfflineMeanTimeInSystem"] = -1
logjson["results"]["AnalysisOfflineIOPSSavedRatio"] = -1
logjson["results"]["AnalysisOfflineEpisodesAdmitted"] = -1
logjson["results"]["AnalysisOfflineCacheSize"] = -1
logjson["results"]["AnalysisAdjWR"] = -1
logjson["results"]["AnalysisAdjServiceTimeSavedRatio"] = -1
logjson["results"]["AnalysisAdjIOPSSavedRatio"] = -1
logjson["results"]["AnalysisAdjMeanTimeInSystem"] = -1
logjson["results"]["AnalysisAdjEpisodesAdmitted"] = -1
logjson["results"]["AnalysisAdjCacheSize"] = -1
if self.df_analysis is not None:
row_analysis, row_analysis_adjusted = None, None
try:
row_analysis = utils.closest_row(self.df_analysis, 'Target Write Rate', logjson["results"]["FlashWriteRate"]).to_dict()
logjson["results"]["AnalysisOfflineWR"] = float(row_analysis["Write Rate (MB/s)"])
logjson["results"]["AnalysisOfflineServiceTimeSavedRatio"] = float(row_analysis["Service Time Saved Ratio"])
logjson["results"]["AnalysisOfflineMeanTimeInSystem"] = float(row_analysis["Mean Time In System (s)"])
logjson["results"]["AnalysisOfflineIOPSSavedRatio"] = float(row_analysis["IOPSSavedRatio"])
logjson["results"]["AnalysisOfflineEpisodesAdmitted"] = int(row_analysis["Episodes admitted"])
logjson["results"]["AnalysisOfflineCacheSize"] = float(row_analysis["Cache Size (GB)"])
# Test serializability
json.dumps(logjson["results"])
row_analysis_adjusted = utils.closest_row(self.df_analysis, 'Target Write Rate', logjson["results"]["AnalysisAdmittedWriteRate"]).to_dict()
logjson["results"]["AnalysisAdjWR"] = float(row_analysis_adjusted["Write Rate (MB/s)"])
logjson["results"]["AnalysisAdjServiceTimeSavedRatio"] = float(row_analysis_adjusted["Service Time Saved Ratio"])
logjson["results"]["AnalysisAdjIOPSSavedRatio"] = float(row_analysis_adjusted["IOPSSavedRatio"])
logjson["results"]["AnalysisAdjMeanTimeInSystem"] = float(row_analysis_adjusted["Mean Time In System (s)"])
logjson["results"]["AnalysisAdjEpisodesAdmitted"] = int(row_analysis_adjusted["Episodes admitted"])
logjson["results"]["AnalysisAdjCacheSize"] = float(row_analysis_adjusted["Cache Size (GB)"])
json.dumps(logjson["results"])
except Exception as e:
print(str(e))
print(row_analysis)
print(row_analysis_adjusted)
traceback.print_exc()
self.df_analysis = None
logjson["results"]["ServiceFetchIOs"] = ods.get("fetches_ios")
logjson["results"]["ServiceFetchChunks"] = ods.get("fetches_chunks")
logjson["results"]["ServiceFetchChunksDemandmiss"] = ods.get("fetches_chunks_demandmiss")
logjson["results"]["ServiceFetchChunksPrefetch"] = ods.get("fetches_chunks_prefetch")
logjson["results"]["ServicePutIOs"] = ods.get("puts_ios")
logjson["results"]["ServicePutChunks"] = ods.get("puts_chunks")
logjson["results"]["ServiceGetPutIOs"] = ods.get("fetches_ios") + ods.get("puts_ios")
logjson["results"]["ServiceGetPutChunks"] = ods.get("fetches_chunks") + ods.get("puts_chunks")
logjson["results"]["BackendBandwidthGet"] = utils.mb_per_sec(logjson["results"]["ServiceFetchChunks"], secs_so_far, sample_ratio)
logjson["results"]["BackendBandwidthPut"] = utils.mb_per_sec(logjson["results"]["ServicePutChunks"], secs_so_far, sample_ratio)
logjson["results"]["BackendBandwidth"] = utils.mb_per_sec(logjson["results"]["ServiceGetPutChunks"], secs_so_far, sample_ratio)
# Lost hits
# admits_at_ep_start
logjson["results"]["WastedHitsAfterEpStart"] = ods.get("flashcache/admits_after_ep_start")
logjson["results"]["WastedHitsAfterEpStartIOs"] = ods.get("flashcache/admits_after_ep_start_ios")
logjson["results"]["WastedHitsAfterEpStartIOsRatio"] = utils.safe_div(
logjson["results"]["WastedHitsAfterEpStartIOs"], iops_requests_sofar)
logjson["results"]["WastedHitsAfterEpStartServiceTimeRatio"] = utils.safe_div(
service_time(ods.get("flashcache/admits_after_ep_start_ios"), ods.get("flashcache/admits_after_ep_start")),
logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["WastedHitsAfterEpStartServiceTimeRatioIOs"] = utils.safe_div(
service_time(ods.get("flashcache/admits_after_ep_start_ios"), 0),
logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["WastedHitsAfterEpStartServiceTimeRatioChunks"] = utils.safe_div(
service_time(0, ods.get("flashcache/admits_after_ep_start")),
logjson["results"]["ServiceTimeTotalOrig"])
# log_items += ods.get_all_with_prefix("flashcache/admitted_without_hits_remaining")
# Real wasted:
# - Readmission (with no more hits, on last seen): should not be admitted
# - Readmission (with more hits - not registered in wasted): EA/etc should be adjusted to consider the cost of extra write. Might be considered in chunkaware.
# - Not In Episode / Prefetch mistakes. Should not be admitted.
# Wasted writes
logjson["results"]["WastedFlashRatio"] = utils.safe_div(logjson["results"]["NumNoHitEvictions"], chunks_written)
logjson["results"]["FlashRatioEAHitsLeft"] = utils.safe_div(logjson["results"]["AssumedFlashEaTooLong"], chunks_written)
# Evicted with future hits: need to be readmitted. EA should match ReadmissionEp.
# ReadmissionEp can overlap with Doomed, esp DoomedOnLastSeen, but not DoomedOneChunkAcc(?).
# Readmissions can be split into:
# - no future hits = (DoomedOnLastSeen - DoomedOneChunkAcc) = DoomedOnLastSeenReadmit
# - with more hits = ReadmissionEp - (DoomedOnLastSeen - DoomedOneChunkAcc)
logjson["results"]["WastedFlashRatioEvictEANoHits"] = utils.safe_div(ods.get("flashcache/evicted_with_hits_remaining_nohits"), chunks_written)
logjson["results"]["WastedFlashRatioEvictEA"] = utils.safe_div(ods.get("flashcache/evicted_with_hits_remaining"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitReadmissionEp"] = utils.safe_div(ods.get("flashcache/readmission_from_ep"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitDoomedOnLastSeenReadmit"] = utils.safe_div(ods.get("flashcache/admitted_doomed_onlastseen_readmissionEp"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitReadmissionEpHidden"] = logjson["results"]["WastedFlashRatioAdmitReadmissionEp"] - logjson["results"]["WastedFlashRatioAdmitDoomedOnLastSeenReadmit"]
# TODO: Fix or remove. Incomplete.
# logjson["results"]["WastedFlashRatioAdmitReadmissionSim"] = utils.safe_div(ods.get("flashcache/admitted_readmission"), chunks_written)
# Doomed - No future hits
logjson["results"]["WastedFlashRatioAdmitDoomed"] = utils.safe_div(ods.get("flashcache/admitted_doomed"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitDoomedOnLastSeen"] = utils.safe_div(ods.get("flashcache/admitted_doomed_onlastseen"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitDoomed1ChunkAcc"] = utils.safe_div(ods.get("flashcache/admitted_doomed_1chunkacc"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitDoomed2ChunkAcc"] = utils.safe_div(ods.get("flashcache/admitted_doomed_2chunkacc"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitDoomed3ChunkAcc"] = utils.safe_div(ods.get("flashcache/admitted_doomed_3chunkacc"), chunks_written)
logjson["results"]["WastedFlashRatioAdmitDoomedOneAcc"] = utils.safe_div(ods.get("flashcache/admitted_doomed_oneacc"), chunks_written)
# Also doomed, but not classified as such
logjson["results"]["WastedFlashRatioAdmitNotInEpisode"] = utils.safe_div(ods.get("flashcache/admitted_chunknotinepisode"), chunks_written)
# Orthogonal category
logjson["results"]["WastedFlashRatioEvictPrefetch"] = utils.safe_div(ods.get("flashcache/evicted_nohits_prefetch"), chunks_written)
flash_wasted_breakdown_admit = ''
flash_wasted_breakdown_evict = []
for k, v in logjson["results"].items():
if k.startswith("WastedFlashRatioAdmit") and k != "WastedFlashRatioAdmit" and v > 0:
flash_wasted_breakdown_admit += f"\n {k.replace('WastedFlashRatioAdmit','')} - {v:.3f}"
elif k.startswith("WastedFlashRatioEvict") and k != "WastedFlashRatioEvict" and v > 0:
flash_wasted_breakdown_evict.append(f"{k.replace('WastedFlashRatioEvict','')}: {v:.3f}")
flash_wasted_breakdown_evict = ', '.join(flash_wasted_breakdown_evict)
logjson["results"]["TotalChunkQueries"] = chunk_queries
logjson["results"]["TotalChunkSaved"] = ods.get("chunks_saved")
# Total Prefetches
prefetches_exists = ods.get("flashcache/prefetches_failed_exists_incache")
prefetches_ = logjson["results"][("RamCache" if self.ram_cache else "") + "NumPrefetches"]
# + prefetches_exists
wasted_pf_ram = ods.get("flashcache/rejections_no_hit_in_ram_prefetches")
wasted_pf_flash = ods.get("flashcache/evicted_nohits_prefetch")
wasted_prefetches = wasted_pf_ram + wasted_pf_flash
useful_pf_ram = ods.get(["chunks_saved", "ram_prefetch_firsthit"])
useful_pf_flash = ods.get(["chunks_saved", "flash_prefetch_firsthit"])
useful_prefetches = useful_pf_ram + useful_pf_flash
logjson["results"]["TotalUsefulPrefetches"] = useful_prefetches
logjson["results"]["TotalWastedPrefetches"] = wasted_prefetches
logjson["results"]["WastedPrefetchRatio"] = utils.safe_div(wasted_prefetches, wasted_prefetches + useful_prefetches)
for k, v in CACHE_LOCATIONS.items():
logjson["results"][f"ServiceTimeSavedFrom{v}"] = service_time(
logjson["results"][f"TotalIOPSSaved{v}"],
logjson["results"][f"TotalChunkSaved{v}"])
logjson["results"][f"ServiceTimeSaved{v}Ratio"] = utils.safe_div(
logjson["results"][f"ServiceTimeSavedFrom{v}"],
logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["ServiceTimeSaved"] = service_time(
logjson["results"]["TotalIOPSSaved"],
logjson["results"]["TotalChunkSaved"] - prefetches_)
logjson["results"]["ServiceTimeSavedRatio"] = utils.safe_div(logjson["results"]["ServiceTimeSaved"], logjson["results"]["ServiceTimeTotalOrig"])
st_keys = {1: '', 2: '2', 3: '3'}
st_stats_nocache = np.diff(ods.get('service_time_nocache_stats'), prepend=0)
st_stats_puts = np.diff(ods.get("service_time_writes_stats"), prepend=0)
if self.skip_first_secs:
intervals_skip = int(self.skip_first_secs // logjson['options']['log_interval'])
if len(st_stats_nocache) > intervals_skip + 1:
st_stats_nocache = st_stats_nocache[intervals_skip:]
st_stats_puts = st_stats_puts[intervals_skip:]
st_nocache_with_put = np.add(st_stats_nocache, st_stats_puts)
logjson["results"]["ServiceTimePutUtil"] = st_to_util(ods.get("service_time_writes"), **util_kwargs) * 100
logjson["results"]["PeakServiceTimePutUtil"] = st_to_util(max(st_stats_puts, default=0), **util_peak_kwargs) * 100
for percentile in [.5, .9, .95, .99, .995, .999, .9999, .99999]:
if len(st_stats_nocache) > 0:
logjson['results'][f'P{percentile*100:g}ServiceTimeUsedNoCache'] = np.percentile(st_stats_nocache, percentile*100)
logjson['results'][f'P{percentile*100:g}ServiceTimeNoCacheUtil'] = st_to_util(logjson['results'][f'P{percentile*100:g}ServiceTimeUsedNoCache'], **util_peak_kwargs) * 100
logjson['results'][f'P{percentile*100:g}ServiceTimeUsedWithPutNoCache'] = np.percentile(st_nocache_with_put, percentile*100)
if len(st_stats_puts) > 0:
logjson["results"][f"P{percentile*100:g}ServiceTimePut"] = np.percentile(st_stats_puts, percentile*100)
logjson["results"][f"P{percentile*100:g}ServiceTimePutUtil"] = st_to_util(
logjson["results"][f"P{percentile*100:g}ServiceTimePut"], **util_peak_kwargs) * 100
for k, v in st_keys.items():
for kk, vv in {"": "used", "OnPut": "writes"}.items():
st_ = ods.get(f"service_time_used{v}")
logjson["results"][f"ServiceTimeUsed{kk}{k}"] = st_
logjson["results"][f"ServiceTime{kk}Util{k}"] = st_to_util(st_, **util_kwargs) * 100
if k == 1:
logjson["results"][f"ServiceTimeUsedWithPut{k}"] = ods.get("service_time")
logjson["results"][f"ServiceTimeWithPutUtil{k}"] = st_to_util(ods.get("service_time"), **util_kwargs) * 100
logjson["results"][f"ServiceTimeOnPrefetchRatio{k}"] = utils.safe_div(ods.get(f"service_time_used_prefetch{v}"), ods.get(f"service_time_used{v}"))
logjson["results"][f"ServiceTimeSavedRatio{k}"] = 1. - utils.safe_div(ods.get(f"service_time_used{v}"), logjson["results"]["ServiceTimeTotalOrig"])
st_stats = np.diff(ods.get(f"service_time_used{v}_stats"), prepend=0)
if self.skip_first_secs and len(st_stats) > intervals_skip + 1:
st_stats = st_stats[intervals_skip:]
logjson["results"][f"PeakServiceTimeUsed{k}"] = max(st_stats, default=0)
logjson["results"][f'PeakServiceTimeSavedRatio{k}'] = 1 - utils.safe_div(logjson["results"][f"PeakServiceTimeUsed{k}"], max(st_stats_nocache, default=0))
logjson["results"][f"PeakServiceTimeUtil{k}"] = st_to_util(max(st_stats), **util_peak_kwargs) * 100
assert len(st_stats) == len(st_stats_puts)
st_with_put = np.add(st_stats, st_stats_puts)
logjson["results"][f"PeakServiceTimeUsedWithPut{k}"] = max(st_with_put, default=0)
logjson["results"][f"PeakServiceTimeUsedWithPutUtil{k}"] = st_to_util(max(st_with_put, default=0), **util_peak_kwargs) * 100
logjson["results"][f'PeakServiceTimeSavedWithPutRatio{k}'] = 1 - utils.safe_div(logjson["results"][f"PeakServiceTimeUsedWithPut{k}"], max(st_nocache_with_put, default=0))
for percentile in [.5, .9, .95, .99, .995, .999, .9999, .99999]:
logjson['results'][f'P{percentile*100:g}ServiceTimeUsed{k}'] = np.percentile(st_stats, percentile*100)
logjson['results'][f'P{percentile*100:g}ServiceTimeUtil{k}'] = st_to_util(logjson['results'][f'P{percentile*100:g}ServiceTimeUsed{k}'], **util_peak_kwargs) * 100
logjson['results'][f'P{percentile*100:g}ServiceTimeSavedRatio{k}'] = 1 - utils.safe_div(logjson['results'][f'P{percentile*100:g}ServiceTimeUsed{k}'], logjson['results'][f'P{percentile*100:g}ServiceTimeUsedNoCache'])
logjson['results'][f'P{percentile*100:g}ServiceTimeUsedWithPut{k}'] = np.percentile(st_with_put, percentile*100)
logjson['results'][f'P{percentile*100:g}ServiceTimeWithPutUtil{k}'] = st_to_util(logjson['results'][f'P{percentile*100:g}ServiceTimeUsedWithPut{k}'], **util_peak_kwargs) * 100
logjson['results'][f'P{percentile*100:g}ServiceTimeSavedWithPutRatio{k}'] = 1 - utils.safe_div(logjson['results'][f'P{percentile*100:g}ServiceTimeUsedWithPut{k}'], logjson['results'][f'P{percentile*100:g}ServiceTimeUsedWithPutNoCache'])
logjson["results"]["ServiceTimeTotalNew"] = service_time(
iops_requests_sofar - logjson["results"]["TotalIOPSSaved"],
chunk_queries - logjson["results"]["TotalChunkSaved"] + prefetches_)
logjson["results"]["ServiceTimeOnPrefetch"] = service_time(0, prefetches_)
logjson["results"]["ServiceTimeOnPrefetchRatio"] = utils.safe_div(logjson["results"]["ServiceTimeOnPrefetch"], logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["ServiceTimeOnWastedPrefetch"] = service_time(0, wasted_prefetches)
logjson["results"]["ServiceTimeOnWastedPrefetchRatio"] = utils.safe_div(logjson["results"]["ServiceTimeOnWastedPrefetch"], logjson["results"]["ServiceTimeTotalOrig"])
iops_saved_by_prefetch = logjson['results']['TotalIOPSSavedRamPrefetchFirstHit'] + logjson['results']['TotalIOPSSavedFlashPrefetchFirstHit']
logjson["results"]["ServiceTimeSavedByPrefetch"] = service_time(iops_saved_by_prefetch, useful_prefetches)
logjson["results"]["ServiceTimeSavedByPrefetchRatio"] = utils.safe_div(logjson["results"]["ServiceTimeSavedByPrefetch"], logjson["results"]["ServiceTimeTotalOrig"])
logjson["results"]["NetServiceTimeForPrefetch"] = logjson['results']['ServiceTimeSavedByPrefetchRatio'] - logjson['results']['ServiceTimeOnPrefetchRatio']
logjson["results"]["NetServiceTimeForGoodPrefetch"] = logjson["results"]["NetServiceTimeForPrefetch"] + logjson['results']['ServiceTimeOnWastedPrefetchRatio']
logjson["results"]["TotalEpisodesAdmitted"] = ods.get("flashcache/episodes_admitted2")
logjson["results"]["EvictionAvgTTL"] = utils.safe_div(ods.get("flashcache/total_ttl"), chunks_written)
logjson["stats"] = ods.counters
statsjson = {}
statsjson["freq"] = ods.freq
statsjson["batches"] = ods.batches
warmup_time_txt = "unfinished"
if logjson['results']['WarmupFinished']:
warmup_time = logjson['results']['WarmupFinished'] - Timestamp(0, self.trace_stats['start_ts'])
logjson['results']['WarmupTime'] = warmup_time
warmup_time_txt = f"{warmup_time.logical}, {fmt_dur(warmup_time.physical)}"
wall_time = time.time() - self.start_time if self.start_time else 0
logjson["results"]["SimWallClockTime"] = wall_time
logjson["results"]["SimRAMUsage"] = utils.memory_usage()
msg = (
"Results preview: \n "
f"Trace: {logjson['trace_kwargs']} \n "
f"AP: {logjson['options']['ap']}, {self.admission_policy} \n "
f"Prefetching: {self.prefetcher} \n "
f"Eviction Policy: {logjson['options']['eviction_policy']}, {self.cache} \n "
f"Average TTL: {logjson['results']['EvictionAvgTTL']:.2f} s \n "
f"Duration so far: {fmt_dur(secs_so_far)} \n "
f"Duration: {fmt_dur(trace_duration_secs)} ({fmt_dur(logjson['options']['log_interval'])} intervals) \n "
f"Service Time Utilization (%) - {logjson['results']['ServiceTimeWithPutUtil1']:.5f} \n "
f"Service Time Utilization (%) [GET] - {logjson['results']['ServiceTimeUtil1']:.5f} \n "
f"Service Time Utilization (%) [PUT] - {logjson['results']['ServiceTimePutUtil']:.5f} \n "
f"Peak Service Time Utilization (%) - {logjson['results']['PeakServiceTimeUsedWithPutUtil1']:.5f} \n "
f"Peak Service Time Utilization (%) [PUT] - {logjson['results']['PeakServiceTimePutUtil']:.5f} \n "
f"Peak Service Time Utilization (%) [GET] - {logjson['results']['PeakServiceTimeUtil1']:.5f} \n "
f"P99 Service Time Utilization (%) - {logjson['results']['P99ServiceTimeWithPutUtil1']:.5f} \n "
f"P99.9 Service Time Utilization (%) - {logjson['results']['P99.9ServiceTimeWithPutUtil1']:.5f} \n "
f"P99.99 Service Time Utilization (%) - {logjson['results']['P99.99ServiceTimeWithPutUtil1']:.5f} \n "
f"P99 Service Time Utilization (%) [GET] - {logjson['results']['P99ServiceTimeUtil1']:.5f} \n "
f"P99.9 Service Time Utilization (%) [GET] - {logjson['results']['P99.9ServiceTimeUtil1']:.5f} \n "
f"P99.99 Service Time Utilization (%) [GET] - {logjson['results']['P99.99ServiceTimeUtil1']:.5f} \n "
f"P99 Service Time Utilization (%) [GET-NoCache] - {logjson['results']['P99ServiceTimeNoCacheUtil']:.5f} \n "
f"P99.9 Service Time Utilization (%) [GET-NoCache] - {logjson['results']['P99.9ServiceTimeNoCacheUtil']:.5f} \n "
f"P99.99 Service Time Utilization (%) [GET-NoCache] - {logjson['results']['P99.99ServiceTimeNoCacheUtil']:.5f} \n "
f"P99 Service Time Utilization (%) [PUT] - {logjson['results']['P99ServiceTimePutUtil']:.5f} \n "
f"P99.9 Service Time Utilization (%) [PUT] - {logjson['results']['P99.9ServiceTimePutUtil']:.5f} \n "
f"P99.99 Service Time Utilization (%) [PUT] - {logjson['results']['P99.99ServiceTimePutUtil']:.5f} \n "
f"P99 Service Time Saved ratio 1 - {logjson['results']['P99ServiceTimeSavedRatio1']:.5f} \n "
f"P99.9 Service Time Saved ratio 1 - {logjson['results']['P99.9ServiceTimeSavedRatio1']:.5f} \n "
f"P99.99 Service Time Saved ratio 1 - {logjson['results']['P99.99ServiceTimeSavedRatio1']:.5f} \n "
f"Peak Service Time Saved ratio 1 (range) - {logjson['results']['PeakServiceTimeSavedRatio1']:.5f} \n "
f"Peak Service Time Saved ratio 2 (1st M to end) - {logjson['results']['PeakServiceTimeSavedRatio2']:.5f} \n "
f"Service Time Saved ratio 1 (range) - {logjson['results']['ServiceTimeSavedRatio1']:.5f} \n "
f"Service Time Saved ratio 2 (first miss to end) - {logjson['results']['ServiceTimeSavedRatio2']:.5f} \n "
f"Service Time Saved ratio 3 (start to end) - {logjson['results']['ServiceTimeSavedRatio3']:.5f} \n "
f"Service Time Saved ratio - {logjson['results']['ServiceTimeSavedRatio']:.5f} \n "
f"Bonus STS beyond STS(A) (Assumed EA too short) - {logjson['results']['TooShortEaBonusServiceTimeSavedRatio']:.5f} \n "
f"Est Service Time Lost from late/readmits - {logjson['results']['WastedHitsAfterEpStartServiceTimeRatio']:.5f} \n "
f"ST + late/readmits potential - {(logjson['results']['ServiceTimeSavedRatio1']+logjson['results']['WastedHitsAfterEpStartServiceTimeRatio']):.5f} \n "
f"Potential STS(Analysis) from Admitted Episodes - {logjson['results']['AnalysisServiceTimeSavedRatio']:.5f} \n "
f"Service Time Saved (Analysis @ {logjson['results']['AnalysisOfflineWR']:.1f}MB/s, {logjson['results']['AnalysisOfflineCacheSize']:.1f}GB) - {logjson['results']['AnalysisOfflineServiceTimeSavedRatio']:.5f} \n "
f"Service Time Saved (Analysis @ {logjson['results']['AnalysisAdjWR']:.1f}MB/s) - {logjson['results']['AnalysisAdjServiceTimeSavedRatio']:.5f} \n "
f"Service Time Saved By Prefetch ratio - {logjson['results']['ServiceTimeSavedByPrefetchRatio']:.5f} \n "
f"Service Time Saved by DRAM (No Prefetch cost) ratio - {logjson['results']['ServiceTimeSavedRamRatio']:.5f} \n "
f"Service Time Saved by Flash (No Prefetch cost) ratio - {logjson['results']['ServiceTimeSavedFlashNotInRamRatio']:.5f} \n "
f"Service Time Saved (No Prefetch Cost) ratio - {logjson['results']['ServiceTimeSavedRatio'] + logjson['results']['ServiceTimeOnPrefetchRatio']:.5f} \n "
f"Service Time Spent on All Prefetches ratio - {logjson['results']['ServiceTimeOnPrefetchRatio']:.5f} \n "
f"Service Time Spent on Wasted Prefetch ratio - {logjson['results']['ServiceTimeOnWastedPrefetchRatio']:.5f} \n "
f"Net Service Time From Good Prefetching ratio - {logjson['results']['NetServiceTimeForGoodPrefetch']:.5f} \n "
f"Net Service Time From Prefetch ratio - {logjson['results']['NetServiceTimeForPrefetch']:.5f} \n "
f"Potential ST Saved (PF) (Analysis) from Admitted Eps - {logjson['results']['AnalysisServiceTimeSavedFromPrefetchRatio']:.2f} \n "
f"Wasted hits from late/readmits: {logjson['results']['WastedHitsAfterEpStartIOs']} IOs, {logjson['results']['WastedHitsAfterEpStart']} chunks \n "
f"IOPS saved ratio - {logjson['results']['IOPSSavedRatio']:.5f} \n "
f"IOPS saved ratio (DRAM Only) - {logjson['results']['IOPSSavedRamOnlyRatio']:.4f} \n "
f"IOPS saved ratio (Flash Not in DRAM) - {logjson['results']['IOPSSavedFlashNotInRamRatio']:.4f} \n "
f"IOPS saved ratio (DRAMPrefetchFirstHit) - {logjson['results']['IOPSSavedRamPrefetchFirstHitRatio']:.4f} \n "
f"IOPS saved ratio (FlashPrefetchFirstHit) - {logjson['results']['IOPSSavedFlashPrefetchFirstHitRatio']:.4f} \n "
f"IOPS saved ratio (DRAM) - {logjson['results']['IOPSSavedRamRatio']:.4f} \n "
f"IOPS saved ratio (Flash) - {logjson['results']['IOPSSavedFlashRatio']:.4f} \n "
f"IOPS saved ratio (AdmitBuffer) - {logjson['results']['IOPSSavedAdmitBufferRatio']:.4f} \n "
f"IOPS saved ratio (DRAMPrefetch) - {logjson['results']['IOPSSavedRamPrefetchRatio']:.4f} \n "
f"IOPS saved ratio (FlashPrefetch) - {logjson['results']['IOPSSavedFlashPrefetchRatio']:.4f} \n "
f"IOPS saved ratio (Partial) - {logjson['results']['IOPSPartialHitsRatio']:.5f} \n "
f"IOPS saved ratio lost from late/readmits - {logjson['results']['WastedHitsAfterEpStartIOsRatio']:.5f} \n "
f"IOPS SR (Analysis) from Admitted Episodes - {logjson['results']['AnalysisIOPSSavedRatio']:.5f} \n "
f"IOPS SR (Analysis, ConstantThisEA) - {logjson['results']['AnalysisOfflineIOPSSavedRatio']:.5f} \n "
f"GETs - {iops_requests_sofar} / {total_iops_get} \n "
f" Saved - {logjson['results']['TotalIOPSSaved']} \n "
f" Misses - {logjson['results']['ServiceFetchIOs']} \n ")
for k, v in ods.get_all_with_prefix("iops_requests/op/"):
msg += f" {k} - {v} \n "
msg += f"GET (No Cache) ST - {st_to_util(ods.get('service_time_nocache'), **util_kwargs)*100:.1f}% \n "
for k, v in ods.get_all_with_prefix("service_time_nocache/op/"):
msg += f" {k} - {st_to_util(v, **util_kwargs)*100:.1f}% \n "
msg += f"PUTs - {ods.get('puts_ios')} \n "
for k, v in ods.get_all_with_prefix("puts_ios/op/"):
msg += f" {k} - {v} \n "
msg += f"PUT ST - {st_to_util(ods.get('service_time_writes'), **util_kwargs)*100:.1f}% \n "
for k, v in ods.get_all_with_prefix("service_time_writes/op/"):
msg += f" {k} - {st_to_util(v, **util_kwargs)*100:.1f}% \n "
# Too verbose for release. TODO: Make this an option.
# for kk in ["ns", "user"]:
# msg += f"IOs by {kk} \n "
# for k, v in ods.get_all_with_prefix(f"iops_requests/{kk}/"):
# msg += f" {k} - {v} \n "
# msg += f"GET (No Cache) ST - {st_to_util(ods.get('service_time_nocache'), **util_kwargs)*100:.1f}% \n "
# for k, v in ods.get_all_with_prefix(f"service_time_nocache/{kk}/"):
# msg += f" {k} - {st_to_util(v, **util_kwargs)*100:.1f}% \n "
# msg += f"PUTs - {ods.get('puts_ios')} \n "
# for k, v in ods.get_all_with_prefix(f"puts_ios/{kk}/"):
# msg += f" {k} - {v} \n "
# msg += f"PUT ST - {st_to_util(ods.get('service_time_writes'), **util_kwargs)*100:.1f}% \n "
# for k, v in ods.get_all_with_prefix(f"service_time_writes/{kk}/"):
# msg += f" {k} - {st_to_util(v, **util_kwargs)*100:.1f}% \n "
msg += (
f"Episodes admitted - {logjson['results']['TotalEpisodesAdmitted']} (Analysis: {logjson['results']['AnalysisOfflineEpisodesAdmitted']}) \n "
f"Chunk hit ratio - {logjson['results']['ChunkHitRatio']:.5f} \n "
f"Chunk hit ratio (DRAM) - {logjson['results']['ChunkHitRamRatio']:.5f} \n "
f"Chunk hit ratio (Flash) - {logjson['results']['ChunkHitFlashRatio']:.5f} \n "
f"Chunk hit ratio (Flash Not In DRAM) - {logjson['results']['ChunkHitFlashNotInRamRatio']:.5f} \n "
f"Flash Cache Hit Rate - {logjson['results']['FlashCacheHitRate']:.5f} \n "
f"Client Bandwidth [GET] - {logjson['results']['ClientBandwidth']:.2f} MB/s \n "
f"Backend Bandwidth [GET+PUT] - {logjson['results']['BackendBandwidth']:.2f} MB/s \n "
f"Backend Bandwidth [GET] - {logjson['results']['BackendBandwidthGet']:.2f} MB/s \n "
f"Backend Bandwidth [PUT] - {logjson['results']['BackendBandwidthPut']:.2f} MB/s \n "
f"Chunks Queried - {chunk_queries} \n "
f"Chunks Saved - {logjson['results']['TotalChunkSaved']} \n "
f"Chunks Fetched from Backend - {logjson['results']['ServiceFetchChunks']} \n "
f" Demand [Not Prefetch] - {logjson['results']['ServiceFetchChunksDemandmiss']} \n"
f" Prefetch - {logjson['results']['ServiceFetchChunksPrefetch']} \n ")
if prefetcher_enabled:
msg += (
f"Prefetches - {prefetches_} \n "
f" Good - {useful_prefetches} \n "
f" Wasted - {wasted_prefetches} ({logjson['results']['WastedPrefetchRatio']:.4f}) \n "
f" Breakdown - DRAM: {wasted_pf_ram}, Flash: {wasted_pf_flash} \n "
f" Failed (Exists) - {prefetches_exists} \n ")
msg += (
f"Flash writes - {chunks_written} \n "
f"Acceptance Ratio - {logjson['results']['AcceptanceRatio']:.5f} \n "
f"Flash write rate - {logjson['results']['FlashWriteRate']:.2f} MB/s \n "
f"Writing Chunks from Admitted Episodes - {logjson['results']['AnalysisAdmittedWriteRate']:.1f} MB/s (WR Ratio: {logjson['results']['AnalysisAdmittedChunkRatio']:.2f}) \n "
f"Analysis Closest Write Rate - {logjson['results']['AnalysisOfflineWR']:.2f} \n "
f"Flash Wasted % of WR - {logjson['results']['WastedFlashRatio']:.2f} \n "
f"Flash Wasted (by admit) - {flash_wasted_breakdown_admit} \n "
f"Flash Wasted (by evict) - {flash_wasted_breakdown_evict} \n "
f"Flash prefetch ratio - {utils.safe_div(logjson['results']['FlashPrefetchWriteRate'], logjson['results']['FlashWriteRate']):.2f} \n "
f"Flash prefetch write rate - {logjson['results']['FlashPrefetchWriteRate']:.2f} MB/s \n "
f"Cache Size - {logjson['results']['AnalysisOfflineCacheSize']:.2f} GB (Analysis) vs {logjson['options']['size_gb']:.2f} (Sim, {logjson['results']['NumCacheElems']} items) \n "
f"Flash Avg Eviction Age - {logjson['results']['AvgEvictionAge']:.1f}\n "
f"Assumed Flash EA \n "
f" Too Short/Bonus Hits - {logjson['results']['AssumedFlashEaTooShort']} (STSR: {logjson['results']['TooShortEaBonusServiceTimeSavedRatio']:.2f}) \n "
f" Too Long/Extra Writes - {logjson['results']['AssumedFlashEaTooLong']} (WRR: {logjson['results']['FlashRatioEAHitsLeft']:.2f}) \n "
f"Flash Mean Time in System - {logjson['results']['MeanTimeInSystem']:.1f} \n "
f"Analysis Mean Time in System - {fmt_dur(logjson['results']['AnalysisOfflineMeanTimeInSystem'], v=2)} \n ")
if self.ram_cache:
msg += (
f"DRAM Cache Hit Rate - {logjson['results']['RamCacheHitRate']:.5f} \n "
f"DRAM writes - {logjson['results']['RamChunkWritten']} \n "
f"DRAM write rate - {logjson['results']['RamWriteRate']:.2f} MB/s \n "
f"DRAM Prefetch write rate - {logjson['results']['RamPrefetchWriteRate']:.2f} MB/s \n "
f"DRAM prefetches - {logjson['results']['RamCacheNumPrefetches']} \n "
f"DRAM failed (exists) prefetches - {logjson['results']['RamCacheNumFailedPrefetchesExists']} \n "
f"DRAM prefetch success rate - {logjson['results']['RamCachePrefetchSuccessRate']} \n "
f"DRAM Cache Avg Eviction Age - {logjson['results']['RamCacheAvgEvictionAge']}\n ")
msg += (
f"Time to warmup - {warmup_time_txt} \n "
f"Simulator RAM usage - {logjson['results']['SimRAMUsage']:.1f} GB \n "
f"Simulator Time - {fmt_dur(wall_time)} \n ")
word_filters = []
if not self.ram_cache:
word_filters.append("dram")
if not prefetcher_enabled:
word_filters.append("prefetch")
msg = msg.split("\n")
for word in word_filters:
msg = [line for line in msg if word.lower() not in line.lower()]
col_width = max(len(line.split(" - ")[0]) for line in msg if " - " in line)
def alignleft(line):
if ' - ' not in line:
return line
line = line.split(' - ')
line[0] = f'{line[0]:<{col_width}}'
return ' - '.join(line)
msg = "\n".join(alignleft(line) for line in msg)
if verbose:
print(msg, file=sys.stderr)
logjson_ = utils.stringify_keys(copy.deepcopy(logjson))
os.makedirs(self.output_dir, 0o755, exist_ok=True)
if dump_stats:
# TODO: Check how long this call is taking
statsjson_ = utils.stringify_keys(copy.deepcopy(statsjson))
dump_logjson(statsjson_, self.filename+'.stats'+suffix, verbose=verbose)
# Dump this last because manager will think it is complete once it sees this
dump_logjson(logjson_, self.filename+suffix, verbose=verbose)
if verbose:
print("Command:")
print(logjson['command'])
return self.filename+suffix
def dump_logjson(json_, filename, verbose=False):
if filename.endswith('.lzma'):
compress_json.dump(json_, filename, json_kwargs=dict(indent=2))
else:
with open(filename, "w+") as out:
json.dump(json_, out, indent=2)
if verbose:
print(f"Results written to {filename}", file=sys.stderr)
def simulate_cachelib(cache, accesses):
ts = 0
stats = {"chunk_hits": 0, "chunk_queries": 0, "rejects_clean": 0}
for op, key in accesses:
block_id, chunk_id = key.split("|#|body-0-")
block_id = int(block_id)
chunk_id = int(chunk_id)
k = (block_id, chunk_id+1)
acc_ts = Timestamp(physical=ts, logical=ts)
if op == "GET":
found = cache.find(k, acc_ts)
if found:
stats["chunk_hits"] += 1
stats["chunk_queries"] += 1
elif op == "SET":
if k not in cache.cache:
cache.insert(k, acc_ts, [])
else:
stats["rejects_clean"] += 1
else:
raise NotImplementedError
ts += 1
stats["chunk_hit_ratio"] = utils.safe_div(stats["chunk_hits"], stats["chunk_queries"])
print(stats)
return stats
class CacheSimulator(object):
def __init__(self,
cache,
ram_cache=None,
sample_ratio=None,
prefetcher=None,
sdumper=None,
# admit_chunk_threshold=None,
# block_level=False,
# log_interval=None,
# limit=None,
options=None,
**kwargs):
self.cache = cache
self.insert_cache = ram_cache if ram_cache else cache
self.ram_cache = ram_cache
self.prefetcher = prefetcher
assert sdumper
self.sdumper = sdumper
self.sample_ratio = sample_ratio
self.options = options
self.config = kwargs
assert not self.config.get('block_level', False)
self._init_logs()
self.hooks = defaultdict(list)
if hasattr(cache.ap, "hooks"):
for k, v in cache.ap.hooks.items():
self.hooks[k] += v
def _init_logs(self):
# TODO: Make this configurable.
self.print_every_n_iops = 50000
self.print_every_n_mins = 1
if utils.DEBUG_FLAG() and '--profile' in sys.argv:
from pympler import tracker
self.tr = tracker.SummaryTracker()
self.print_every_n_iops = 10000
self.checkpoints_since_last_increase = 0
self.header_prev = ""
self.col_maxwidth = defaultdict(int)
# For logging granularity, in trace time
self.last_log_tracetime = None
# In wallclock time, for triggering gc, touching lockfile
self.last_syscheck = 0
self.last_print = {'i': None, 'time': 0, 'time_frac': 0, 'io_frac': 0, 'tracetime_elapsed': 0}
self.start_ts = None
# TODO: Refactor
self.last_util_peak = 0
# self.last_util_peak_withput = 0
# self.dumped_for_limit = False
def _syscheck(self):
self.last_syscheck = time.time()
# gc.collect()
self._touch_lockfile()
def _log_prof_memory(self):
if utils.DEBUG_FLAG() and '--profile' in sys.argv:
from pympler import muppy
self.tr.print_diff()
all_objs = muppy.get_objects()
leaked_objects1 = muppy.filter(all_objs, Type=list)
leaked_objects2 = muppy.filter(all_objs, Type=tuple)
for _ in range(20):
o1 = random.choice(leaked_objects1)
print("Object:", o1)
referents = muppy.get_referents(o1)
print("Referents: ", len(referents))
if referents:
print("Choice:", random.choice(referents))
for _ in range(20):
o2 = random.choice(leaked_objects2)
print("Object (tuple):", o2)
referents = muppy.get_referents(o2)
print("Referents (tuple): ", len(referents))
if referents:
print("Choice:", random.choice(referents))
def _checkpoint(self, acc_ts, print_log=True, save=True):
"""
_checkpoint serves 2 purposes: period logging, and progress update.
"""
cache = self.cache
prefetcher = self.prefetcher
col_maxwidth = self.col_maxwidth
dur = (acc_ts - self.last_log_tracetime).physical
assert cache.keys_written == cache.evictions + len(cache.cache), f"{cache.keys_written} {cache.evictions} {len(cache.cache)}"
# TODO: Peak mitigation
last_span = ods.last_span("time_phy", init=self.start_ts.physical)
# First check if we have at least a few batches
if "service_time_used_stats" in ods.batches and len(ods.batches["service_time_used_stats"]) > 10:
# 1. Record last peak (also, print it)
# log_items.append(("STGet%$", "{:.2f}", st_to_util(ods.span("service_time_used", i=bi), sample_ratio=self.sample_ratio, duration_s=log_dur) * 100))
prev_peaks = [ods.get_at("service_time_used_stats", x) for x in [-4, -3, -2, -1]]
prev_peaks_writes = [ods.get_at("service_time_writes", x) for x in [-4, -3, -2, -1]]
prev_times = np.diff([ods.get_at("time_phy", x) for x in [-4, -3, -2, -1]])
# print(prev_peaks)
# print(prev_times)
prev_peaks = [st_to_util(v, sample_ratio=self.sample_ratio, duration_s=prev_times[i]) for i, v in enumerate(np.diff(prev_peaks))]
prev_peaks_writes = [st_to_util(v, sample_ratio=self.sample_ratio, duration_s=prev_times[i]) for i, v in enumerate(np.diff(prev_peaks_writes))]
prev_peaks_total = [x+y for x, y in zip(prev_peaks, prev_peaks_writes)]
# print("Recent ST Util (%) peaks, taking max:", prev_peaks)
# print("Prev max: ", self.last_util_peak)
# print(f"ST Util is {stutil_get*100:.1f}%; changing AP threshold to {self.cache.ap.threshold}")
recent_max = max(prev_peaks_total)
self.last_util_peak = max(self.last_util_peak, recent_max)
if self.options.peak_strategy is None:
pass
elif self.options.peak_strategy.startswith("zero_nonpeak"):
if self.options.peak_strategy == "zero_nonpeak6":
if recent_max < 0.5 * self.last_util_peak:
self.cache.ap.threshold = self.options.ap_threshold * 0.1 # Don't admit at all
elif recent_max >= 0.8 * self.last_util_peak:
self.cache.ap.threshold = self.options.ap_threshold * 1.25
else:
self.cache.ap.threshold = self.options.ap_threshold
elif self.options.peak_strategy == "zero_nonpeak5":
if recent_max < 0.3 * self.last_util_peak:
self.cache.ap.threshold = 0 # Don't admit at all
elif recent_max >= 0.8 * self.last_util_peak:
self.cache.ap.threshold = self.options.ap_threshold * 1
else:
self.cache.ap.threshold = self.options.ap_threshold
elif self.options.peak_strategy == "zero_nonpeak4":
if recent_max < 0.5 * self.last_util_peak:
self.cache.ap.threshold = 0 # Don't admit at all
elif recent_max >= 0.8 * self.last_util_peak:
self.cache.ap.threshold = self.options.ap_threshold * 2
else:
self.cache.ap.threshold = self.options.ap_threshold
# print(f"Changing threshold to {self.cache.ap.threshold}")
# if last_span > 0:
# stutil_get = st_to_util(ods.last_span("service_time_used"), sample_ratio=self.sample_ratio, duration_s=last_span)
# if stutil_get < 0.03:
# self.cache.ap.threshold = self.options.ap_threshold / 2
# elif stutil_get > 0.06:
# self.cache.ap.threshold = self.options.ap_threshold * 2
# else:
# self.cache.ap.threshold = self.options.ap_threshold
# print(f"ST Util is {stutil_get*100:.1f}%; changing AP threshold to {self.cache.ap.threshold}")
ods.append("time_phy", acc_ts.physical)
ods.append("time_elapsed_phy", (acc_ts - self.start_ts).physical)
ods.append("time_log", acc_ts.logical)
ods.append("duration", dur / 3600)
ods.append("realtime_elapsed", time.time() - self.realtime_start)
# TODO: Deprecate servicetime_orig, no longer used in this file (just in python notebooks). Replaced by service_time_nocache
ods.append("servicetime_orig", service_time(ods.last_span("iops_requests"), ods.last_span("chunk_queries")))
if prefetcher and prefetcher.pf_range == 'chunk2' and cache.evictions > 0:
prefetcher.assumed_ea = cache.computeEvictionAge()
tracetime_elapsed = acc_ts.physical - self.start_ts.physical
time_frac = tracetime_elapsed / self.total_secs
io_frac = ods.get('iops_requests') / self.total_iops_get
print_log_frac = (io_frac - self.last_print['io_frac']) > .2
print_log_frac = print_log_frac or (tracetime_elapsed - self.last_print['tracetime_elapsed']) > 3600*24
if print_log or print_log_frac:
bi = self.last_print['i']
log_items = []
est_time_left = utils.safe_div(self.total_iops_get - ods.get("iops_requests"),
ods.span("iops_requests", i=bi)) * ods.span("realtime_elapsed", i=bi)
log_items.append(("TimeLeft", fmt_dur(est_time_left, verbose=1, smallest='m')))
log_items.append(("i ", len(ods.get('time_phy', init=[]))))
# log_items.append(("%TimeP", f"{time_frac*100:.1f}"))
log_items.append(("TraceTime", fmt_dur(tracetime_elapsed, v=1, smallest='h')))
log_dur = ods.span("time_phy", i=bi, init=self.start_ts.physical)
log_items.append(("Hrs$", f"{log_dur / 3600:.1f}"))
log_items.append(("%GETs", f"{io_frac*100:.1f}"))
# $ means it was calculated on this log window (Diff-based)
log_items.append(("GETs$", ods.span("iops_requests", i=bi)))
log_items.append(("PUTs$", ods.span("puts_ios", i=bi)))
# TODO: Show PeakST here
log_items.append(("STGet%$", "{:.2f}", st_to_util(ods.span("service_time_used", i=bi), sample_ratio=self.sample_ratio, duration_s=log_dur) * 100))
log_items.append(("PeakST%$", "{:.2f}", self.last_util_peak * 100))
log_items.append(("STGetNoCa%$", "{:.2f}", st_to_util(ods.span("service_time_nocache", i=bi), sample_ratio=self.sample_ratio, duration_s=log_dur) * 100))
log_items.append(("STPut%$", "{:.2f}", st_to_util(ods.span("service_time_writes", i=bi), sample_ratio=self.sample_ratio, duration_s=log_dur) * 100))
# log_items.append(("STGet$", ods.span("service_time_used", fmt="{:.1f}", i=bi)[1]))
# log_items.append(("STPut$", ods.span("service_time_writes", fmt="{:.1f}", i=bi)[1]))
wr_k = {
'ReqMBs$': 'chunk_queries',
'GetMBs$': 'fetches_chunks',
'PutMBs$': 'puts_chunks',
'FlaMBs$': 'flashcache/keys_written',
# 'EvictMBps$': 'flashcache/evictions',
'PreMBs$': 'flashcache/prefetches',
}
for log_key, stat_key in wr_k.items():
if log_key != 'PrefetchMBps$' or (prefetcher and prefetcher.enabled):
v = utils.mb_per_sec(ods.span(stat_key, i=bi), log_dur, self.sample_ratio)
log_items.append([log_key, "{:.2f}", v])
# log_items.append(ods.last("servicetime_saved_ratio", "{:.4f}"))
sts_ratio = 1. - utils.safe_div(ods.span("service_time_used", i=bi), ods.span("service_time_nocache", i=bi))
log_items.append(("STSaved%$", "{:.1f}%", sts_ratio * 100))
if prefetcher and prefetcher.enabled:
sts_pf_ratio = utils.safe_div(ods.span("service_time_used_prefetch", i=bi), ods.span("service_time_nocache", i=bi))
log_items.append(("STPref%$", "{:.1f}%", sts_pf_ratio * 100))
# st_new = ods.span("service_time_nocache", i=bi) - ods.span("servicetime_saved", i=bi)
# st_new = ods.span("service_time_used", i=bi)
# log_items.append(("STNew$", "{:.1f}".format(st_new)))
log_items.append(("Accept%$", "{:.2f}%", 100*utils.safe_div(
ods.span("flashcache/keys_written", i=bi),
ods.span("flashcache/rejections", i=bi) + ods.span("flashcache/keys_written", i=bi),
)))
l_ios_late = "flashcache/admits_after_ep_start_ios"
l_chunks_late = "flashcache/admits_after_ep_start"
st_late = service_time(ods.span(l_ios_late, i=bi), ods.span(l_chunks_late, i=bi))
# log_items.append(("STLate$", f"{st_late:.1f}"))
st_late_ratio = utils.safe_div(st_late, ods.span("service_time_nocache", i=bi))
log_items.append(("STLate%$", "{:.1f}%", st_late_ratio * 100))
# log_items.append(("IOsLate$", ods.span(l_ios_late, i=bi)))
# log_items.append(("ChunksLate$", ods.span(l_chunks_late, i=bi)))
# TODO: Restore.
# for location in CACHE_LOCATIONS:
# ods.append(f"iops_saved{location}_ratio", utils.safe_div(
# stats["iops_saved" + location][stats_idx],
# stats["iops_requests"][stats_idx]))
# ods.append(f"iops_partial{location}_ratio", utils.safe_div(
# stats["iops_partial_hits" + location][stats_idx],
# stats["iops_requests"][stats_idx]))
# for k in CACHE_LOCATIONS:
# if "ram" in k and self.ram_cache is None:
# continue
# if "prefetch" in k and not (prefetcher and prefetcher.enabled):
# continue
# log_items.append(ods.last(f"iops_saved{k}_ratio", "{:.5f}" if k == "" else "{:.4f}"))
# log_items.append(ods.last("iops_partial_ratio", "{:.4f}"))
wasted_rate_t = utils.safe_div(
ods.span("flashcache/unaccessed_evictions", i=bi),
ods.span("flashcache/evictions", i=bi))
log_items.append(("Wasted%$", "{:.1f}%", 100 * wasted_rate_t))
# wasted_rate_all = utils.safe_div(cache.un_accessed_evictions, cache.evictions)
# log_items.append(("WastedRate", f"{wasted_rate_all:.4f}"))
# ods.append("max_ia_max", cache.computeMaxMaxInterarrivalTime())
# cache.max_max_interarrival_time = 0 # Hack to make it hour-specific.
# log_items.append(("EA", f"{cache.computeEvictionAge():.1f}"))
ea_t = utils.safe_div(ods.span("flashcache/eviction_age_cum", i=bi, init=Timestamp(0, 0)),
ods.span("flashcache/evictions", i=bi))
log_items.append(("EA$", f"{ea_t:.0f}"))
# log_items.append(ods.last("max_ia_max", "{:.2f}"))
# AvgIAMax is close to AvgIAMaxEvicted
log_items.append(("AvgIAMax", f"{cache.computeAvgMaxInterarrivalTime():.0f}"))
# log_items.append(("AvgIAMaxEvicted", f"{cache.computeAvgMaxInterarrivalTimeEvicted():.1f}"))
# ods.append("eage_nohit", cache.computeNoHitEvictionAge())
# ods.append("cache_avg_object_size", cache.computeAvgObjectSize())
# log_items.append(ods.last("duration", "{:.1f}"))
# for k in ["", "_ram", "_flash_noram", "_flash"]:
# if "ram" not in k or self.ram_cache is not None:
# log_items.append(ods.last(f"chunk_hits{k}_ratio", "{:.2f}"))
# log_items += ods.get_all_with_prefix("flashcache/evicted_")
log_items.append(("RAMGb", f"{utils.memory_usage():.1f}"))
log_items.append(("EstTotalTime", fmt_dur(est_time_left + ods.span("realtime_elapsed", i=0), verbose=1, smallest='m')))
log_items.append(("Speedup$", "{:.2f}", utils.safe_div(log_dur * self.sample_ratio / 100, ods.span("realtime_elapsed", i=bi))))
log_items.append(("GET/Ts$", "{:.1f}", utils.safe_div(ods.span("iops_requests", i=bi), log_dur)))
log_items.append(("GET/s$", "{:.1f}", utils.safe_div(ods.span("iops_requests", i=bi), ods.span("realtime_elapsed", i=bi))))
if ods.get("ml_batches") > 0:
log_items.append(("MLbatch/s$", "{:.1f}", utils.safe_div(ods.span("ml_batches", i=bi), ods.span("realtime_elapsed", i=bi))))
log_items.append(("MLpred/s$", "{:.1f}", utils.safe_div(ods.span("ml_predictions", i=bi), ods.span("realtime_elapsed", i=bi))))
log_items += ods.get_all_with_prefix("flashcache/admitted_after_eps_start")
# log_items += ods.get_all_with_prefix("flashcache/admitted_chunknotinepisode")
log_items += ods.get_all_with_prefix("flashcache/admitted_without_hits_remaining")
log_items += ods.get_all_with_prefix("warning_")
log_items += ods.get_all_with_prefix("flashcache/warning_")
log_items += ods.get_all_with_prefix("ramcache/warning_")
# print(f" | {cache_avg_object_size:.2f}")
# Dump to file
hdr = [row[0] for row in log_items]
log_items_ = [row[1].format(*row[2:]) if len(row) > 2 else row[1] for row in log_items]
def shorten(v):
v = v.replace("_this", "$")
v = utils.to_camelcase(v)
return v
hdr = [f'[{i}] {shorten(v)}' for i, v in enumerate(hdr)]
log_items_ = [f'[{i}] {v}' for i, v in enumerate(log_items_)]
# Padding
for i, hd in enumerate(hdr):
col_maxwidth[hd] = max(col_maxwidth[hd], len(log_items_[i]))
if len(hd) < 20:
col_maxwidth[hd] = max(col_maxwidth[hd], len(hd))
hdr[i] = hd.ljust(col_maxwidth[hd])
log_items_[i] = log_items_[i].ljust(col_maxwidth[hd])
header = " | ".join(hdr)
log_str = " | ".join(log_items_)
# cntr = cache.ap.seen_before
# print(Counter(cntr.values()))
# Problem with * is that if it's not registered, it won't appear.
ods.checkpoint_many([
"service_time",
"service_time_used*",
"service_time_nocache*",
"service_time_writes*",
"fetches_*",
"puts_*",
"iops_requests", "chunk_queries",
"time_phy", "time_log",
"realtime_elapsed", "ml_batches", "ml_predictions",
"flashcache/keys_written", "flashcache/prefetches",
"flashcache/eviction_age_cum", "flashcache/evictions",
"flashcache/unaccessed_evictions", "flashcache/unaccessed_eviction_age_cum",
"flashcache/admits_after_ep_start", "flashcache/admits_after_ep_start_ios"])
if print_log or print_log_frac:
self.last_print['i'] = len(ods.get("time_phy", init=[])) - 1
self.last_print['io_frac'] = io_frac
self.last_print['time_frac'] = time_frac
self.last_print['tracetime_elapsed'] = tracetime_elapsed
self._syscheck()
if header != self.header_prev:
print(header, file=sys.stderr)
self.header_prev = header
print(log_str, file=sys.stderr)
if print_log:
self.checkpoints_since_last_increase += 1
if self.checkpoints_since_last_increase >= 5 and self.print_every_n_mins < 10:
self.print_every_n_mins = min(10, self.print_every_n_mins * 2)
print(f"(Increasing print interval to {self.print_every_n_mins} mins)")
self.checkpoints_since_last_increase = 0
if self.sdumper and save:
# Only dump stats on the first one (to check it works)
self.sdumper.dump(None,
suffix=".part.lzma",
dump_stats=self.last_print['time'] == 0)
# Put this after dump because dumping is slow
self.last_print['time'] = time.time()
self._syscheck()
self._log_prof_memory()
# if limit is not None and iops_sofar/total_iops > limit and not dumped_for_limit:
# dumped_for_limit = True
# sdumper.dump(stats, verbose=True, suffix=".limit.lzma")
# if False:
# return stats
self.last_log_tracetime = acc_ts
assert len(ods.get("service_time_writes_stats")) == len(ods.get("service_time_nocache_stats"))
ods.idx = int((acc_ts - self.start_ts).physical // self.config['log_interval'])