-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws_main.c
1565 lines (1380 loc) · 48.9 KB
/
ws_main.c
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
/*-----------------------------------------------------------------------*/
/*--- A Valgrind tool to compute working sets of a process. ws_main.c ---*/
/*-----------------------------------------------------------------------*/
/*
This file is part of ws.
Copyright (C) 2018 Martin Becker
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include <sys/types.h>
#include "pub_tool_basics.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_libcassert.h"
#include "pub_tool_libcprint.h"
#include "pub_tool_libcbase.h"
#include "pub_tool_libcfile.h"
#include "pub_tool_libcproc.h"
#include "pub_tool_options.h"
#include "pub_tool_machine.h" // VG_(fnptr_to_fnentry)
#include "pub_tool_clientstate.h" // args + exe name
#include "pub_tool_hashtable.h"
#include "pub_tool_mallocfree.h"
#include "pub_tool_debuginfo.h"
#include "pub_tool_stacktrace.h"
#include "pub_tool_execontext.h"
#include "pub_tool_threadstate.h"
#include "pub_tool_xtree.h"
#include "pub_tool_xarray.h"
#include "valgrind.h"
/*------------------------------------------------------------*/
/*--- version-specific defs ---*/
/*------------------------------------------------------------*/
//#define DEBUG
#if defined(__VALGRIND_MAJOR__) && defined(__VALGRIND_MINOR__) \
&& (__VALGRIND_MAJOR__ >= 3 && __VALGRIND_MINOR__ >= 14)
// all okay
#else
#error "This valgrind version is too old"
#endif
/*------------------------------------------------------------*/
/*--- tool info ---*/
/*------------------------------------------------------------*/
#define WS_NAME "ws"
#define WS_VERSION "0.4"
#define WS_DESC "compute working set for data and instructions"
/*------------------------------------------------------------*/
/*--- type definitions ---*/
/*------------------------------------------------------------*/
typedef unsigned long pagecount;
struct map_pageaddr
{
VgHashNode top; // page address, must be first
unsigned long int count;
Time last_access;
DiEpoch ep; // FIXME: opt: we do not use debug info for data pages, remove ep for data?
};
#define vgPlain_malloc(size) vgPlain_malloc ((const char *) __func__, size)
typedef enum { TimeI, TimeMS } TimeUnit;
typedef
IRExpr
IRAtom;
/* --- Operations --- */
typedef enum { OpLoad=0, OpStore=1, OpAlu=2 } Op;
#define MAX_DSIZE 512
typedef
enum { Event_Ir, Event_Dr, Event_Dw, Event_Dm }
EventKind;
typedef
struct {
EventKind ekind;
IRAtom* addr;
Int size;
IRAtom* guard; /* :: Ity_I1, or NULL=="always True" */
}
Event;
typedef
struct {
Time t; // FIXME: opt: we could store only the delta to the intended point in time.
pagecount pages_insn;
pagecount pages_data;
#ifdef DEBUG
Float mAvg, mVar;
#endif
}
WorkingSet;
/**
* @brief details for a single working set sample
*/
typedef
struct {
unsigned int id;
unsigned int cnt;
HChar *callstack;
}
SampleInfo;
/**
* @brief element in hash table ExeContext -> SampleInfo.
*/
struct map_context2sampleinfo
{
VgHashNode top; // ExeContext ECU, must be first
SampleInfo info;
};
/**
* @brief element in list ws_context_list
*/
typedef
struct {
Time t;
ExeContext *ec;
}
SampleContext;
/**
* @brief internal data of peak detector
*/
typedef
struct {
// states
unsigned k;
short int peak_pre;
Float filt_pre;
Float movingAvg;
Float movingVar;
// params
unsigned window;
Float threshgain;
Float adaptrate; ///< coefficient for filtering out peaks
Float exp_alpha; ///< coefficient for exponential moving filters
}
PeakDetect;
typedef
struct {
HChar *str;
int rem;
}
callstack_string;
typedef
struct {
unsigned long n;
unsigned long sum;
Addr pre;
}
LocalityInfo;
/*------------------------------------------------------------*/
/*--- prototypes ---*/
/*------------------------------------------------------------*/
static void maybe_compute_ws (void);
/*------------------------------------------------------------*/
/*--- globals ---*/
/*------------------------------------------------------------*/
static Bool postmortem = False; ///< certain actions we cannot do after process terminated
static Long guest_instrs_executed = 0;
// page access tables
static VgHashTable *ht_data;
static VgHashTable *ht_insn;
static VgHashTable *ht_ec2sampleinfo;
// list of user-defined points in time where sample info shall be recorded
static XArray *ws_info_times;
static int next_user_time_idx = -1;
// working set at each point in time
static XArray *ws_at_time;
static unsigned long drop_samples = 0;
// list of sample contexts; on termination converted to SampleInfo
static XArray *ws_context_list;
// locality info
LocalityInfo locality_insn, locality_data;
static ULong n_SBs_entered = 0;
static ULong n_SBs_exited = 0;
/* Up to this many unnotified events are allowed. Must be at least two,
so that reads and writes to the same address can be merged into a modify.
Beyond that, larger numbers just potentially induce more spilling due to
extending live ranges of address temporaries. */
#define N_EVENTS 4
/* Maintain an ordered list of memory events which are outstanding, in
the sense that no IR has yet been generated to do the relevant
helper calls. The SB is scanned top to bottom and memory events
are added to the end of the list, merging with the most recent
notified event where possible (Dw immediately following Dr and
having the same size and EA can be merged).
This merging is done so that for architectures which have
load-op-store instructions (x86, amd64), the instr is treated as if
it makes just one memory reference (a modify), rather than two (a
read followed by a write at the same address).
At various points the list will need to be flushed, that is, IR
generated from it. That must happen before any possible exit from
the block (the end, or an IRStmt_Exit). Flushing also takes place
when there is no space to add a new event, and before entering a
RMW (read-modify-write) section on processors supporting LL/SC.
If we require the simulation statistics to be up to date with
respect to possible memory exceptions, then the list would have to
be flushed before each memory reference. That's a pain so we don't
bother.
Flushing the list consists of walking it start to end and emitting
instrumentation IR for each event, in the order in which they
appear. */
static Event events[N_EVENTS];
static Int events_used = 0;
PeakDetect pd_data, pd_insn;
/*------------------------------------------------------------*/
/*--- Command line options ---*/
/*------------------------------------------------------------*/
/* Command line options controlling instrumentation kinds, as described at
* the top of this file. */
#define WS_DEFAULT_PS 4096
#define WS_DEFAULT_EVERY 100000
#define WS_DEFAULT_TAU WS_DEFAULT_EVERY
#define WS_DEFAULT_PEAKT 5
#define WS_DEFAULT_PEAKW 30
#define WS_DEFAULT_PEAKADP 0.25f ///< default value for peak filter. lower=more robust to bursts
// user inputs:
static Bool clo_locations = True;
static Bool clo_listpages = False;
static Bool clo_peakdetect = False;
static Bool clo_localitytr = False;
static Int clo_peakthresh = WS_DEFAULT_PEAKT; // FIXME: Float?
static Int clo_peakwindow = WS_DEFAULT_PEAKW;
static Float clo_peakadapt = WS_DEFAULT_PEAKADP; // FIXME: from clo
static Int clo_pagesize = WS_DEFAULT_PS;
static Int clo_every = WS_DEFAULT_EVERY;
static Int clo_tau = 0;
static Int clo_time_unit = TimeI;
/* The name of the function of which the number of calls (under
* --basic-counts=yes) is to be counted, with default. Override with command
* line option --fnname. */
//static const HChar* clo_fnname = "main";
static const HChar* clo_filename = "ws.out.%p";
static const HChar* clo_info_at = "";
static HChar* int_filename;
/*------------------------------------------------------------*/
/*--- libc replacements ---*/
/*------------------------------------------------------------*/
#define FABS(x) ((x < 0.f) ? -x : x)
static inline
Float exp_approx(Float x)
{
x = 1.0 + x / 256.0;
x *= x; x *= x; x *= x; x *= x;
x *= x; x *= x; x *= x; x *= x;
return x;
}
/*------------------------------------------------------------*/
/*--- all other functions ---*/
/*------------------------------------------------------------*/
static
Bool ws_process_cmd_line_option(const HChar* arg)
{
if VG_BOOL_CLO(arg, "--ws-locations", clo_locations) {}
else if VG_BOOL_CLO(arg, "--ws-list-pages", clo_listpages) {}
else if VG_STR_CLO(arg, "--ws-file", clo_filename) {}
else if VG_STR_CLO(arg, "--ws-info-at", clo_info_at) {}
else if VG_INT_CLO(arg, "--ws-pagesize", clo_pagesize) {}
else if VG_INT_CLO(arg, "--ws-every", clo_every) {}
else if VG_INT_CLO(arg, "--ws-tau", clo_tau) { tl_assert(clo_tau > 0); }
else if VG_XACT_CLO(arg, "--ws-time-unit=i", clo_time_unit, TimeI) {}
else if VG_XACT_CLO(arg, "--ws-time-unit=ms", clo_time_unit, TimeMS) {}
else if VG_BOOL_CLO(arg, "--ws-peak-detect", clo_peakdetect) {}
else if VG_BOOL_CLO(arg, "--ws-track-locality", clo_localitytr) {}
else if VG_INT_CLO(arg, "--ws-peak-window", clo_peakwindow) { tl_assert(clo_peakwindow > 0); }
else if VG_INT_CLO(arg, "--ws-peak-thresh", clo_peakthresh) { tl_assert(clo_peakthresh > 0); }
else return False;
tl_assert(clo_filename);
tl_assert(clo_filename[0]);
tl_assert(clo_pagesize > 0);
tl_assert(clo_every > 0);
return True;
}
static
void ws_print_usage(void)
{
VG_(printf)(
" --ws-file=<string> file name to write results\n"
" --ws-list-pages=no|yes print list of all accessed pages [no]\n"
" --ws-locations=no|yes collect location info for insn pages in listing [yes]\n"
" --ws-peak-detect=no|yes collect info for peaks in working set [no]\n"
" --ws-peak-window=<int> window length (in samples) for peak detection [%d]\n"
" --ws-peak-thresh=<int> threshold for peaks. Lower is more sensitive [%d]\n"
" --ws-info-at=<int>(,<int>)* list of points in time where additional information shall be recorded\n"
" --ws-track-locality=no|yes compute locality of access\n"
" --ws-pagesize=<int> size of VM pages in bytes [%d]\n"
" --ws-time-unit=i|ms time unit: instructions executed (default), milliseconds\n"
" --ws-every=<int> sample working set every <int> time units [%d]\n"
" --ws-tau=<int> consider all accesses made in the last tau time units [%d]\n",
WS_DEFAULT_PEAKW,
WS_DEFAULT_PEAKT,
WS_DEFAULT_PS,
WS_DEFAULT_EVERY,
WS_DEFAULT_TAU
);
}
static
void ws_print_debug_usage(void)
{
VG_(printf)(
" (none)\n"
);
}
static
const HChar* TimeUnit_to_string(TimeUnit time_unit)
{
switch (time_unit) {
case TimeI: return "instructions";
case TimeMS: return "ms";
default: tl_assert2(0, "TimeUnit_to_string: unrecognised TimeUnit");
}
}
static
void init_locality(LocalityInfo *li)
{
li->sum = 0;
li->n = 0;
li->pre = 0;
}
static
void init_peakd(PeakDetect *pd)
{
pd->filt_pre = 0.f;
pd->peak_pre = 0;
pd->k = 0;
pd->movingAvg = 0.f;
pd->movingVar = 0.f;
pd->window = clo_peakwindow;
pd->exp_alpha = 2.f / (clo_peakwindow + 1);
pd->adaptrate = (Float) clo_peakadapt;
pd->threshgain = (Float) clo_peakthresh;
}
/**
* @brief detects peaks in working set size
* Implements a moving average (avg) and a moving variance (var) filter.
* Window length is given by --ws-peak-window.
* Peak is detected if signal properties change such that a certain
* metric becomes higher than a threshold.
*
* The Fano factor F (var/avg) is used to decide how peaks are detected:
* - high F: compare changes to current ver
* - low F: compare changes to current avg
* - in between: smooth transition between both
*
* Additionally, the signal is exponentially filtered iff peaks are detected,
* before it is taken into the moving windows. Full filtering (1.0) means
* the signal is assumed stationary, and thresholds do not react to peaks. Vice
* versa, 0.0 suppresses filtering, taking signal into account as it is.
*/
static
Bool peak_detect(PeakDetect *pd, pagecount y)
{
short int pk = 0;
Float filt = (Float) y;
// detect peaks and filter them
Float coeff = 1.0;
if (pd->movingAvg > 0.f) {
const Float fano = pd->movingVar / pd->movingAvg;
coeff = 1.0 - exp_approx(-fano / 2.); // fano = 1.0 -> 60% weight to variance
}
const Float thresh = coeff * pd->threshgain * pd->movingVar +
(1.f - coeff) * pd->threshgain/10. * pd->movingAvg;
Float y0 = y - pd->movingAvg;
const Bool is_peak = FABS(y0) > thresh;
if (pd->k >= pd->window && is_peak) {
pk = (y > pd->movingAvg) ? 1 : -1;
filt = pd->adaptrate * y + (1 - pd->adaptrate) * pd->filt_pre;
}
// moving variance (must be calc'd first)
if (0 < pd->k) {
const Float diff = ((Float) filt) - pd->movingAvg; // XXX: important, previous average.
pd->movingVar = (1.f - pd->exp_alpha) * (pd->movingVar + pd->exp_alpha * diff * diff);
} else {
pd->movingVar = 0.f;
}
// moving avg
if (0 < pd->k) {
pd->movingAvg = pd->exp_alpha * filt + (1.f - pd->exp_alpha) * pd->movingAvg;
} else {
pd->movingAvg = (Float) filt;
}
if (pd->k < pd->window) pd->k++;
pd->filt_pre = filt;
Bool ret = pk != pd->peak_pre;
pd->peak_pre = pk;
return ret;
}
/**
* @brief Get current time, in whatever time unit we're using.
*/
static
Time get_time(void)
{
if (clo_time_unit == TimeI) {
return guest_instrs_executed;
} else if (clo_time_unit == TimeMS) {
// Some stuff happens between the millisecond timer being initialised
// to zero and us taking our first snapshot. We determine that time
// gap so we can subtract it from all subsequent times so that our
// first snapshot is considered to be at t = 0ms. Unfortunately, a
// bunch of symbols get read after the first snapshot is taken but
// before the second one (which is triggered by the first allocation),
// so when the time-unit is 'ms' we always have a big gap between the
// first two snapshots. But at least users won't have to wonder why
// the first snapshot isn't at t=0.
static Bool is_first_get_time = True;
static Time start_time_ms;
if (is_first_get_time) {
start_time_ms = VG_(read_millisecond_timer)();
is_first_get_time = False;
return 0;
} else {
return VG_(read_millisecond_timer)() - start_time_ms;
}
} else {
tl_assert2(0, "bad --ws-time-unit value");
}
}
static
inline Addr pageaddr(Addr addr)
{
return addr & ~(clo_pagesize-1);
}
static
inline void track_locality(LocalityInfo *li, Addr addr)
{
if (li->pre != addr) {
const unsigned long d = li->pre > addr? li->pre - addr : addr - li->pre;
li->sum += d;
li->pre = addr;
}
li->n++; ///< technically, we could derive this from #page accesses. But it's ~no overhead.
}
// TODO: pages shared between processes?
static
inline void pageaccess(Addr pageaddr, VgHashTable *ht)
{
// this is a one-item cache, exploiting locality and speeding up sim dramatically
static Addr lastaddr = 0;
static struct map_pageaddr *lastpage = NULL;
struct map_pageaddr *page;
if (pageaddr == lastaddr) {
page = lastpage;
} else {
page = VG_(HT_lookup) (ht, pageaddr); // FIXME: might be a bottleneck
if (page == NULL) {
page = VG_(malloc) (sizeof (*page));
page->top.key = pageaddr;
page->count = 0;
page->ep = VG_(current_DiEpoch)();
VG_(HT_add_node) (ht, (VgHashNode *) page);
}
lastaddr = pageaddr;
lastpage = page;
}
page->count++;
page->last_access = (long) get_time();
maybe_compute_ws();
}
static
VG_REGPARM(2) void trace_data(Addr addr, SizeT size)
{
const Addr pa = pageaddr(addr);
pageaccess(pa, ht_data);
if (clo_localitytr) track_locality(&locality_data, addr);
}
static
VG_REGPARM(2) void trace_instr(Addr addr, SizeT size)
{
const Addr pa = pageaddr(addr);
pageaccess(pa, ht_insn);
if (clo_localitytr) track_locality(&locality_insn, addr);
}
static
void flushEvents(IRSB* sb)
{
Int i;
const HChar* helperName;
void* helperAddr;
IRExpr** argv;
IRDirty* di;
Event* ev;
for (i = 0; i < events_used; i++) {
ev = &events[i];
// Decide on helper fn to call and args to pass it.
switch (ev->ekind) {
case Event_Ir: helperName = "trace_instr";
helperAddr = trace_instr; break;
case Event_Dr:
case Event_Dw:
case Event_Dm: helperName = "trace_data";
helperAddr = trace_data; break;
default:
tl_assert(0);
}
// Add the helper. FIXME: help the branch predictor here?
argv = mkIRExprVec_2( ev->addr, mkIRExpr_HWord( ev->size ));
di = unsafeIRDirty_0_N( /*regparms*/2,
helperName, VG_(fnptr_to_fnentry)( helperAddr ),
argv );
if (ev->guard) {
di->guard = ev->guard;
}
addStmtToIRSB( sb, IRStmt_Dirty(di) );
}
events_used = 0;
}
static
void addEvent_Ir ( IRSB* sb, IRAtom* iaddr, UInt isize )
{
Event* evt;
tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB)
|| VG_CLREQ_SZB == isize );
if (events_used == N_EVENTS)
flushEvents(sb);
tl_assert(events_used >= 0 && events_used < N_EVENTS);
evt = &events[events_used];
evt->ekind = Event_Ir;
evt->addr = iaddr;
evt->size = isize;
evt->guard = NULL;
events_used++;
}
/* Add a guarded read event. */
static
void addEvent_Dr_guarded ( IRSB* sb, IRAtom* daddr, Int dsize, IRAtom* guard )
{
Event* evt;
tl_assert(isIRAtom(daddr));
tl_assert(dsize >= 1 && dsize <= MAX_DSIZE);
if (events_used == N_EVENTS)
flushEvents(sb);
tl_assert(events_used >= 0 && events_used < N_EVENTS);
evt = &events[events_used];
evt->ekind = Event_Dr;
evt->addr = daddr;
evt->size = dsize;
evt->guard = guard;
events_used++;
}
/* Add an ordinary read event, by adding a guarded read event with an
always-true guard. */
static
void addEvent_Dr ( IRSB* sb, IRAtom* daddr, Int dsize )
{
addEvent_Dr_guarded(sb, daddr, dsize, NULL);
}
/* Add a guarded write event. */
static
void addEvent_Dw_guarded ( IRSB* sb, IRAtom* daddr, Int dsize, IRAtom* guard )
{
Event* evt;
tl_assert(isIRAtom(daddr));
tl_assert(dsize >= 1 && dsize <= MAX_DSIZE);
if (events_used == N_EVENTS)
flushEvents(sb);
tl_assert(events_used >= 0 && events_used < N_EVENTS);
evt = &events[events_used];
evt->ekind = Event_Dw;
evt->addr = daddr;
evt->size = dsize;
evt->guard = guard;
events_used++;
}
/* Add an ordinary write event. Try to merge it with an immediately
preceding ordinary read event of the same size to the same
address. */
static
void addEvent_Dw ( IRSB* sb, IRAtom* daddr, Int dsize )
{
Event* lastEvt;
Event* evt;
tl_assert(isIRAtom(daddr));
tl_assert(dsize >= 1 && dsize <= MAX_DSIZE);
// Is it possible to merge this write with the preceding read?
lastEvt = &events[events_used-1];
if (events_used > 0
&& lastEvt->ekind == Event_Dr
&& lastEvt->size == dsize
&& lastEvt->guard == NULL
&& eqIRAtom(lastEvt->addr, daddr))
{
lastEvt->ekind = Event_Dm;
return;
}
// No. Add as normal.
if (events_used == N_EVENTS)
flushEvents(sb);
tl_assert(events_used >= 0 && events_used < N_EVENTS);
evt = &events[events_used];
evt->ekind = Event_Dw;
evt->size = dsize;
evt->addr = daddr;
evt->guard = NULL;
events_used++;
}
/**
* @brief sort Time
*/
static
Int time_compare (const Time **t1, const Time **t2)
{
if (((long)**t1) > ((long)**t2)) return 1;
if (((long)**t1) < ((long)**t2)) return -1;
return 0;
}
static
void ws_post_clo_init(void)
{
// ensure we have a separate file for every process
const HChar *append = ".%p";
const int appendlen = (VG_(strstr) (clo_filename, "%p") == NULL) ? VG_(strlen) (append) : 0;
int_filename = (HChar*) VG_(malloc) (VG_(strlen) (clo_filename) + 1 + appendlen);
VG_(strcpy) (int_filename, clo_filename);
if (appendlen > 0) VG_(strcat) (int_filename, append);
VG_(umsg)("Output file: %s\n", int_filename);
// check intervals and times
if (clo_tau == 0) clo_tau = clo_every;
if (clo_time_unit != TimeI) {
VG_(umsg)("Warning: time unit %s not implemented, yet. Fallback to instructions",
TimeUnit_to_string(clo_time_unit));
clo_time_unit = TimeI;
}
// user list of times for sample info
{
// parse
const HChar *pt = clo_info_at;
UInt num;
while (VG_(parse_UInt) (&pt, &num)) {
Time *it = VG_(malloc) (sizeof(Time));
if (it) {
*it = (Time)num;
VG_(addToXA) (ws_info_times, &it);
}
while (*pt == ',' || *pt == ' ') pt++;
}
// sort and un-dupe
VG_(setCmpFnXA) (ws_info_times, (XACmpFn_t) time_compare);
VG_(sortXA) (ws_info_times);
int num_t = VG_(sizeXA) (ws_info_times);
Time pre;
for (int i = 0; i < num_t; /* in loop*/) {
Time **cur = VG_(indexXA)(ws_info_times, i);
if (i > 0 && pre == **cur) {
pre = **cur;
VG_(removeIndexXA) (ws_info_times, i);
num_t--;
} else {
pre = **cur;
++i;
}
}
// summarize
num_t = VG_(sizeXA) (ws_info_times);
if (num_t > 0) {
next_user_time_idx = 0;
VG_(umsg) ("Recording info at user times: ");
for (int i = 0; i < num_t; i++) {
Time **t = VG_(indexXA)(ws_info_times, i);
VG_(umsg) ("%ld ", (long)**t);
}
VG_(umsg) ("\n");
}
}
// peak filters
init_peakd(&pd_data);
init_peakd(&pd_insn);
// locality trackers
init_locality(&locality_data);
init_locality(&locality_insn);
// verbose a bit
VG_(umsg)("Page size = %d bytes\n", clo_pagesize);
VG_(umsg)("Computing WS every %d %s\n", clo_every,
TimeUnit_to_string(clo_time_unit));
VG_(umsg)("Considering references in past %d %s\n", clo_tau,
TimeUnit_to_string(clo_time_unit));
}
static
void add_one_SB_entered(void)
{
n_SBs_entered++;
}
static
void add_one_SB_exited(void)
{
n_SBs_exited++;
}
/**
* @brief instruments SB with a counter that increments by n, and is saved
* into guest_instrs_executed.
*/
static
void add_counter_update(IRSB* sbOut, Int n)
{
#if defined(VG_BIGENDIAN)
#define END Iend_BE
#elif defined(VG_LITTLEENDIAN)
#define END Iend_LE
#else
#error "Unknown endianness"
#endif
// Add code to increment 'guest_instrs_executed' by 'n', like this:
// WrTmp(t1, Load64(&guest_instrs_executed))
// WrTmp(t2, Add64(RdTmp(t1), Const(n)))
// Store(&guest_instrs_executed, t2)
IRTemp t1 = newIRTemp(sbOut->tyenv, Ity_I64);
IRTemp t2 = newIRTemp(sbOut->tyenv, Ity_I64);
IRExpr* counter_addr = mkIRExpr_HWord( (HWord)&guest_instrs_executed );
IRStmt* st1 = IRStmt_WrTmp(t1, IRExpr_Load(END, Ity_I64, counter_addr));
IRStmt* st2 =
IRStmt_WrTmp(t2,
IRExpr_Binop(Iop_Add64, IRExpr_RdTmp(t1),
IRExpr_Const(IRConst_U64(n))));
IRStmt* st3 = IRStmt_Store(END, counter_addr, IRExpr_RdTmp(t2));
addStmtToIRSB( sbOut, st1 );
addStmtToIRSB( sbOut, st2 );
addStmtToIRSB( sbOut, st3 );
}
// iterate pages and count those accessed within (now_time - tau, now_time)
static
unsigned long recently_used_pages(VgHashTable *ht, Time now_time)
{
unsigned long cnt = 0;
Time tmin = 0;
if (clo_tau < now_time) tmin = now_time - clo_tau;
VG_(HT_ResetIter)(ht);
const VgHashNode *nd;
while ((nd = VG_(HT_Next)(ht))) {
const struct map_pageaddr *page = (const struct map_pageaddr *) nd;
if (page->last_access > tmin) cnt++;
}
return cnt;
}
/**
* @brief actually assemble callstack string
* * XXX: do not change this function without according changes in strstack_maxlen()
*/
static
void strstack_make
(UInt n, DiEpoch ep, Addr ip, void* uu_opaque)
{
callstack_string *cs = (callstack_string*) uu_opaque;
// inlining
InlIPCursor *iipc = VG_(new_IIPC)(ep, ip);
do {
const HChar *fname;
UInt line;
if (VG_(get_filename_linenum)(ep, ip, &fname, NULL, &line)) {
// format: "%s:%u"
VG_(strncat) (cs->str, fname, cs->rem); cs->rem -= VG_(strlen)(fname);
VG_(strncat) (cs->str, ":", cs->rem); cs->rem -= 1;
cs->rem -= VG_(snprintf) (cs->str + VG_(strlen) (cs->str), cs->rem, "%u", line);
}
// separator
VG_(strncat) (cs->str, "|", cs->rem); cs->rem -= 1;
} while (VG_(next_IIPC)(iipc));
}
/**
* @brief determine max. length of callstack string
* XXX: do not change this function without according changes in strstack_make()
*/
static
void strstack_maxlen(UInt n, DiEpoch ep, Addr ip, void* uu_opaque)
{
callstack_string *cs = (callstack_string*) uu_opaque;
// inlining
InlIPCursor *iipc = VG_(new_IIPC)(ep, ip);
do {
const HChar *fname;
UInt line;
if (VG_(get_filename_linenum)(ep, ip, &fname, NULL, &line)) {
// format: "%s:%u"
HChar buf[8];
cs->rem += VG_(strlen) (fname);
cs->rem += 1;
cs->rem += VG_(snprintf) (buf, sizeof(buf), "%u", line);
}
cs->rem += 1; // trailing separator or NULL terminator.
} while (VG_(next_IIPC)(iipc));
VG_(delete_IIPC)(iipc);
}
/**
* @brief get callstack as string
*/
static
HChar* get_callstack(ExeContext *ec)
{
callstack_string cs;
const DiEpoch ep = VG_(get_ExeContext_epoch) (ec);
// pre-calculate length of string
cs.rem = 0;
VG_(apply_ExeContext) (strstack_maxlen, (void*)&cs, ep, ec);
cs.rem += 1; // better than sorry
cs.str = (HChar*) VG_(malloc) (cs.rem * sizeof(HChar));
cs.str[0] = 0;
// assemble string
VG_(apply_ExeContext) (strstack_make, (void*)&cs, ep, ec);
cs.str[VG_(strlen) (cs.str) - 1] = 0; // remove trailing separator
return cs.str;
}
/**
* @brief record additional information about process right now
*/
static
void record_sample_info(Time now_time)
{
SampleContext *wsp = VG_(malloc) (sizeof(*wsp));
if (wsp) {
wsp->t = now_time;
if (!postmortem) {
ThreadId tid = VG_(get_running_tid)();
wsp->ec = VG_(record_ExeContext)(tid, 0);
} else {
wsp->ec = VG_(null_ExeContext)();
}
VG_(addToXA) (ws_context_list, &wsp);
}
}
static
void compute_ws(Time now_time)
{
/*********
* WSS
*********/
WorkingSet *ws = VG_(malloc) (sizeof(WorkingSet));
if (!ws) {
drop_samples++;
return;
}
ws->t = now_time;
ws->pages_insn = recently_used_pages (ht_insn, now_time);
ws->pages_data = recently_used_pages (ht_data, now_time);
VG_(addToXA) (ws_at_time, &ws);
/*********
* INFO
*********/
Bool have_info = False;
if (next_user_time_idx >= 0) {
Time **nextt= VG_(indexXA) (ws_info_times, next_user_time_idx);
if (now_time >= **nextt) {
record_sample_info (now_time);
have_info = True;
// go to next one that is in the future (they might be too dense for --ws-every)
do {
if (next_user_time_idx < VG_(sizeXA)(ws_info_times) - 1) {
next_user_time_idx++;
} else {
next_user_time_idx = -1;
}
if (next_user_time_idx < 0) break;
nextt = VG_(indexXA) (ws_info_times, next_user_time_idx);
} while (**nextt < now_time);
}
}
if (clo_peakdetect) {
// both peak detect have to run every sample for uniformity, thus no short-circuit eval
const Bool pk_data = peak_detect(&pd_data, ws->pages_data);
const Bool pk_insn = peak_detect(&pd_insn, ws->pages_insn);
if (!have_info && (pk_data || pk_insn)) {
record_sample_info (now_time);
}
#ifdef DEBUG
ws->mAvg = pd_data.movingAvg;
ws->mVar = pd_data.movingVar;
#endif
}
}
static
void maybe_compute_ws (void)
{
tl_assert(clo_time_unit == TimeI);
// if 'every' time units have passed, determine working set again
static Time earliest_possible_time_of_next_ws = 0;
Time now_time = get_time();