-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.cpp
1789 lines (1426 loc) · 61.2 KB
/
analyzer.cpp
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
// (C) 2021-2023 by [email protected]
// released under Apache license v2.0
#include "config.h"
#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <error.h>
#include <fcntl.h>
#if HAVE_GVC == 1
#include <gvc.h>
#endif
#include <jansson.h>
#include <map>
#include <math.h>
#include <optional>
#include <set>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <time.h>
#include <vector>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "lock_tracer.h"
std::string resolver = "/usr/bin/eu-addr2line";
std::string core_file, exe_file;
typedef enum { UG_HTML, UG_TEXT, UG_SQL } ug_output_t;
std::string myformat(const char *const fmt, ...)
{
char *buffer = nullptr;
va_list ap;
va_start(ap, fmt);
if (vasprintf(&buffer, fmt, ap) == -1) {
va_end(ap);
return "(?)";
}
va_end(ap);
std::string result = buffer;
free(buffer);
return result;
}
json_t *load_json(const std::string & filename)
{
json_error_t error { 0 };
json_t *rc = json_load_file(filename.c_str(), 0, &error);
if (!rc) {
fprintf(stderr, "Meta data file (dump.dat) broken: %s\n", error.text);
return nullptr;
}
return rc;
}
std::string lock_action_to_name(const lock_action_t la)
{
if (la == a_lock)
return "lock";
else if (la == a_unlock)
return "unlock";
else if (la == a_thread_clean)
return "thread_clean";
else if (la == a_r_lock)
return "r_lock";
else if (la == a_w_lock)
return "w_lock";
else if (la == a_rw_unlock)
return "rw_unlock";
else if (la == a_init)
return "init";
else if (la == a_destroy)
return "destroy";
else if (la == a_rw_init)
return "rw_init";
else if (la == a_rw_destroy)
return "rw_destroy";
return "internal error";
}
const lock_trace_item_t *load_data(const std::string & filename)
{
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Failed opening %s: %s\n", filename.c_str(), strerror(errno));
return nullptr;
}
struct stat st;
fstat(fd, &st);
lock_trace_item_t *data = (lock_trace_item_t *)mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %s\n", strerror(errno));
return nullptr;
}
if (posix_madvise(data, st.st_size, POSIX_MADV_SEQUENTIAL) == -1)
perror("posix_madvise");
close(fd);
return data;
}
const lock_usage_groups_t *load_ug_data(const std::string & filename)
{
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Failed opening %s: %s\n", filename.c_str(), strerror(errno));
return nullptr;
}
struct stat st;
fstat(fd, &st);
lock_usage_groups_t *data = (lock_usage_groups_t *)mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %s\n", strerror(errno));
return nullptr;
}
if (posix_madvise(data, st.st_size, POSIX_MADV_SEQUENTIAL) == -1)
perror("posix_madvise");
close(fd);
return data;
}
typedef uint64_t hash_t;
uint64_t MurmurHash64A(const void *const key, const int len, const uint64_t seed)
{
const uint64_t m = 0xc6a4a7935bd1e995LLU;
const int r = 47;
uint64_t h = seed ^ (len * m);
const uint64_t *data = (const uint64_t *)key;
const uint64_t *end = (len >> 3) + data;
while(data != end) {
uint64_t k = *data++;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
}
const uint8_t *data2 = (const uint8_t *)data;
switch(len & 7) {
case 7: h ^= (uint64_t)(data2[6]) << 48;
case 6: h ^= (uint64_t)(data2[5]) << 40;
case 5: h ^= (uint64_t)(data2[4]) << 32;
case 4: h ^= (uint64_t)(data2[3]) << 24;
case 3: h ^= (uint64_t)(data2[2]) << 16;
case 2: h ^= (uint64_t)(data2[1]) << 8;
case 1: h ^= (uint64_t)(data2[0]);
h *= m;
};
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
hash_t calculate_backtrace_hash(const void *const *const pointers, const size_t n_pointers)
{
// hash the contents of the pointer-array instead of where they point to
return MurmurHash64A((const void *const)pointers, n_pointers * sizeof(void *), 0);
}
// lae_already_locked: already locked by this tid
// lae_not_locked: unlock without lock
// lae_not_owner: other thread unlocks mutex
typedef enum { lae_already_locked = 0, lae_not_locked, lae_not_owner } lock_action_error_t;
constexpr const char *const lock_action_error_str[] = { "already locked", "not locked", "not owner (or not waiting for (r/w-lock))" };
typedef struct {
std::vector<size_t> latest_records;
size_t first_record;
} double_un_lock_t;
template<typename Type>
void put_lock_error(std::map<std::pair<const Type *, lock_action_error_t>, std::map<hash_t, double_un_lock_t> > *const target, const Type *const lock, const lock_action_error_t error_type, const hash_t calltrace_hash, const size_t record_nr)
{
std::pair<const Type *, lock_action_error_t> key { lock, error_type };
auto it = target->find(key);
if (it == target->end()) {
// this kind of error we've not seen earlier with this lock
std::map<hash_t, double_un_lock_t> entry;
double_un_lock_t data;
data.first_record = record_nr;
entry.insert({ calltrace_hash, data });
target->insert({ key, entry });
}
else {
// this error occured earlier with this lock
auto hash_map_it = it->second.find(calltrace_hash);
if (hash_map_it == it->second.end()) {
double_un_lock_t data;
data.first_record = record_nr;
it->second.insert({ calltrace_hash, data });
}
else {
hash_map_it->second.latest_records.push_back(record_nr);
}
}
}
// this may give false positives if for example an other mutex is malloced()/new'd
// over the location of a previously unlocked mutex
typedef struct {
std::set<pid_t> tids;
} lock_record_t;
auto do_find_double_un_locks_mutex(const lock_trace_item_t *const data, const size_t n_records)
{
std::map<std::pair<const pthread_mutex_t *, lock_action_error_t>, std::map<hash_t, double_un_lock_t> > out;
std::map<const pthread_mutex_t *, lock_record_t> locked;
for(size_t i=0; i<n_records; i++) {
const pthread_mutex_t *const mutex = (const pthread_mutex_t *)data[i].lock;
const pid_t tid = data[i].tid;
// ignore calls that failed
if (data[i].rc != 0)
continue;
if (data[i].la == a_lock) {
// see if it is already locked by current 'tid' which is a mistake
auto it = locked.find(mutex);
if (it != locked.end()) {
if (it->second.tids.find(tid) != it->second.tids.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, mutex, lae_already_locked, hash, i);
}
else {
// new locker of this mutex
it->second.tids.insert(tid);
}
}
else {
// new mutex
locked.insert({ mutex, { { tid } } });
}
}
else if (data[i].la == a_unlock) {
// see if it is not locked (mistake)
auto it = locked.find(mutex);
if (it == locked.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, mutex, lae_not_locked, hash, i);
}
// see if it is not locked by current tid (mistake)
else {
auto tid_it = it->second.tids.find(tid);
if (tid_it == it->second.tids.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, mutex, lae_not_owner, hash, i);
}
else {
it->second.tids.erase(tid_it);
}
if (it->second.tids.empty())
locked.erase(it);
}
}
}
return out;
}
std::map<const void *, std::string> symbol_cache;
std::string lookup_symbol(const void *const p)
{
if (p == nullptr)
return "(nil)";
auto it = symbol_cache.find(p);
if (it != symbol_cache.end())
return it->second;
std::string command_line;
if (core_file.empty() == false)
command_line = myformat("%s -x -a -C --core %s %p", resolver.c_str(), core_file.c_str(), p);
else
command_line = myformat("%s -x -a -C -e %s %p", resolver.c_str(), exe_file.c_str(), p);
char buffer[4096] { 0x00 };
FILE *fh = popen(command_line.c_str(), "r");
if (fh) {
if (fread(buffer, 1, sizeof buffer - 1, fh) == 0)
buffer[0] = 0x00;
for(;;) {
char *lf = strchr(buffer, '\n');
if (!lf)
break;
*lf = '/';
}
pclose(fh);
}
else {
fprintf(stderr, "Cannot resolve symbol (\"%s\"): %s\n", command_line.c_str(), strerror(errno));
}
char *lf = strchr(buffer, '\n');
if (lf)
*lf = 0x00;
std::string result = buffer;
if (result.substr(0, 2) == "??" || result == "")
result = myformat("%p", p);
symbol_cache.insert({ p, result });
return result;
}
#if defined(WITH_BACKTRACE)
void put_call_trace_html(FILE *const fh, const lock_trace_item_t & record, const std::string & table_color)
{
fprintf(fh, "<table class=\"%s\">\n", table_color.c_str());
int d = CALLER_DEPTH - 1;
while(d > 0 && record.caller[d] == nullptr)
d--;
for(int i=0; i<=d; i++)
fprintf(fh, "<tr><th>%p</th><td>%s</td></tr>\n", record.caller[i], lookup_symbol(record.caller[i]).c_str());
fprintf(fh, "</table>\n");
}
void put_call_trace_text(FILE *const fh, const lock_trace_item_t & record)
{
int d = CALLER_DEPTH - 1;
while(d > 0 && record.caller[d] == nullptr)
d--;
if (d >= 0) {
fprintf(fh, "\t");
for(int i=0; i<=d; i++)
fprintf(fh, "%p ", record.caller[i]);
fprintf(fh, "%s", lookup_symbol(record.caller[d]).c_str());
}
}
#endif
constexpr uint64_t billion = 1000000000ll;
std::string my_ctime(const uint64_t nts)
{
time_t t = nts / billion;
struct tm tm { 0 };
localtime_r(&t, &tm);
return myformat("%04d-%02d-%02d %02d:%02d:%02d.%09d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, int(nts % billion));
}
void put_record_details_html(FILE *const fh, const lock_trace_item_t & record, const std::string & base_color)
{
fprintf(fh, "<table class=\"%s\">\n", base_color.c_str());
fprintf(fh, "<tr><th>tid</th><td>%d</td></tr>\n", record.tid);
#ifdef STORE_THREAD_NAME
fprintf(fh, "<tr><th>thread name</th><td>%s</td></tr>\n", record.thread_name);
#endif
#ifdef MEASURE_TIMING
fprintf(fh, "<tr><th>action</th><td>%s</td></tr>\n", lock_action_to_name(record.la).c_str());
fprintf(fh, "<tr><th>lock</th><td>%p</td></tr>\n", record.lock);
fprintf(fh, "<tr><th>timestamp</th><td>%s</td></tr>\n", my_ctime(record.timestamp).c_str());
fprintf(fh, "<tr><th>took</th><td>%.3fus</td></tr>\n", record.lock_took / 1000.0);
#endif
#if defined(WITH_BACKTRACE)
fprintf(fh, "<tr><th>call trace</th><td>");
put_call_trace_html(fh, record, base_color);
fprintf(fh, "</td></tr>\n");
#endif
fprintf(fh, "</table>\n");
}
void put_record_details_text(FILE *const fh, const lock_trace_item_t & record)
{
fprintf(fh, "%d", record.tid);
#ifdef STORE_THREAD_NAME
if (record.thread_name[0])
fprintf(fh, "\t%s", record.thread_name);
else
fprintf(fh, "\t-");
#endif
#ifdef MEASURE_TIMING
fprintf(fh, "\t%s", lock_action_to_name(record.la).c_str());
fprintf(fh, "\t%p", record.lock);
fprintf(fh, "\t%s", my_ctime(record.timestamp).c_str());
fprintf(fh, "\t%.3fus", record.lock_took / 1000.);
#endif
#if defined(WITH_BACKTRACE)
put_call_trace_text(fh, record);
#endif
fprintf(fh, "\n");
}
void put_record_details_sql(FILE *const fh, const lock_trace_item_t & record, const uint64_t nr)
{
fprintf(fh, "INSERT INTO lock_trace(nr, tid, thread_name, action, lock, timestamp, took) "
"VALUES(%zu, %d, \"%s\", \"%s\", \"%s\", %.9f, %.6f);\n",
nr,
record.tid,
record.thread_name,
lock_action_to_name(record.la).c_str(),
myformat("%p", record.lock).c_str(),
record.timestamp / 1000000000.,
record.lock_took / 1000.);
}
std::map<hash_t, size_t> find_a_record_for_unique_backtrace_hashes(const lock_trace_item_t *const data, const std::vector<size_t> & backtraces)
{
std::map<hash_t, size_t> out;
for(auto i : backtraces) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
auto it = out.find(hash);
if (it == out.end())
out.insert({ hash, i });
}
return out;
}
void find_double_un_locks_mutex(FILE *const fh, const lock_trace_item_t *const data, const uint64_t n_records)
{
auto mutex_lock_mistakes = do_find_double_un_locks_mutex(data, n_records);
fprintf(fh, "<section>\n");
fprintf(fh, "<h2 id=\"doublem\">4. mutex lock/unlock mistakes</h2>\n");
fprintf(fh, "<p>Mistakes are: locking a mutex another time by the same thread, unlocking mutexes that are not locked and unlocking of a mutex by some other thread than the one who locked the mutex.</p>\n");
fprintf(fh, "<p>This section contains a list of all the seen mutex/error-type combinations and then for each the mistakes made and then one or more backtraces (\"first\" and \"next\") where they occured.</p>\n");
fprintf(fh, "<p>Count: %zu</p>\n", mutex_lock_mistakes.size());
for(auto mutex_lock_mistake : mutex_lock_mistakes) {
fprintf(fh, "<h3>mutex %p, type \"%s\"</h3>\n", (const void *)mutex_lock_mistake.first.first, lock_action_error_str[mutex_lock_mistake.first.second]);
for(auto map_entry : mutex_lock_mistake.second) {
double_un_lock_t & dul = map_entry.second;
// first (correct?)
if (dul.latest_records.empty() == false)
fprintf(fh, "<h4>first</h4>\n");
put_record_details_html(fh, data[dul.first_record], "red");
// then list all mistakes for this combination, show only unique backtraces
if (dul.latest_records.empty() == false) {
fprintf(fh, "<h4>next</h4>\n");
fprintf(fh, "<p>Mistake count: %zu (total number of backtraces seen; note that the list below is de-duplicated).</p>\n", dul.latest_records.size());
auto unique_backtraces = find_a_record_for_unique_backtrace_hashes(data, dul.latest_records);
for(auto entry : unique_backtraces)
put_record_details_html(fh, data[entry.second], "red");
}
fprintf(fh, "<br>\n");
}
}
fprintf(fh, "</section>\n");
}
std::map<int, std::vector<size_t> > do_list_fuction_call_errors(const lock_trace_item_t *const data, const uint64_t n_records)
{
std::map<int, std::vector<size_t> > errors;
for(size_t i=0; i<n_records; i++) {
if (data[i].rc == 0)
continue;
auto it = errors.find(data[i].rc);
if (it == errors.end())
errors.insert({ data[i].rc, { i } });
else
it->second.push_back(i);
}
return errors;
}
void list_fuction_call_errors(FILE *const fh, const lock_trace_item_t *const data, const uint64_t n_records)
{
auto error_list = do_list_fuction_call_errors(data, n_records);
fprintf(fh, "<section>\n");
fprintf(fh, "<h2 id=\"errors\">3. function call errors</h2>\n");
fprintf(fh, "<p>pthread_-functions can fail, they then return an errno-alike error code. In this section, all that occured (for the ones checked, like mutex errors etc) are listed.</p>\n");
fprintf(fh, "<p>Count: %zu</p>\n", error_list.size());
for(auto it : error_list) {
fprintf(fh, "<h3>%s</h3>\n", strerror(it.first));
auto unique_backtraces = find_a_record_for_unique_backtrace_hashes(data, it.second);
for(auto entry : unique_backtraces) {
put_record_details_html(fh, data[entry.second], "green");
fprintf(fh, "<br>\n");
}
}
fprintf(fh, "</section>\n");
}
std::map<const pthread_mutex_t *, std::vector<size_t> > do_find_still_locked_mutex(const lock_trace_item_t *const data, const uint64_t n_records)
{
std::map<const pthread_mutex_t *, int> mutexes_counts;
std::map<const pthread_mutex_t *, std::vector<size_t> > mutexes_where;
for(size_t i=0; i<n_records; i++) {
// ignore calls that failed
if (data[i].rc != 0)
continue;
const pthread_mutex_t *const mutex = (const pthread_mutex_t *)data[i].lock;
if (data[i].la == a_lock) {
auto it = mutexes_counts.find(mutex);
if (it == mutexes_counts.end()) {
mutexes_counts.insert({ mutex, 1 });
mutexes_where.insert({ mutex, { i } });
}
else {
it->second++;
mutexes_where.find(mutex)->second.push_back(i);
}
}
else if (data[i].la == a_unlock) {
auto it = mutexes_counts.find(mutex);
if (it != mutexes_counts.end()) {
if (it->second > 0)
it->second--;
if (it->second == 0) {
mutexes_counts.erase(mutex);
mutexes_where.erase(mutex);
}
}
}
}
return mutexes_where;
}
void find_still_locked_mutex(FILE *const fh, const lock_trace_item_t *const data, const uint64_t n_records)
{
auto still_locked_list = do_find_still_locked_mutex(data, n_records);
fprintf(fh, "<section>\n");
fprintf(fh, "<h2 id=\"stillm\">5. still locked mutexes</h2>\n");
fprintf(fh, "<p>A list of the mutexes that were still locked when the program terminated.</p>\n");
fprintf(fh, "<p>Count: %zu</p>\n", still_locked_list.size());
for(auto it : still_locked_list) {
fprintf(fh, "<h3>mutex %p</h3>\n", (const void *)it.first);
auto unique_backtraces = find_a_record_for_unique_backtrace_hashes(data, it.second);
if (unique_backtraces.size() == 1)
fprintf(fh, "<p>The following location did not unlock:</p>\n");
else
fprintf(fh, "<p>One of the following locations did not unlock:</p>\n");
for(auto entry : unique_backtraces) {
put_record_details_html(fh, data[entry.second], "blue");
fprintf(fh, "<br>\n");
}
}
fprintf(fh, "</section>\n");
}
std::map<const pthread_rwlock_t *, std::vector<size_t> > do_find_still_locked_rwlock(const lock_trace_item_t *const data, const uint64_t n_records)
{
std::map<const pthread_rwlock_t *, int> rwlocks_counts;
std::map<const pthread_rwlock_t *, std::vector<size_t> > rwlockes_where;
for(size_t i=0; i<n_records; i++) {
// ignore calls that failed
if (data[i].rc != 0)
continue;
const pthread_rwlock_t *const rwlock = (const pthread_rwlock_t *)data[i].lock;
if (data[i].la == a_r_lock || data[i].la == a_w_lock) {
auto it = rwlocks_counts.find(rwlock);
if (it == rwlocks_counts.end()) {
rwlocks_counts.insert({ rwlock, 1 });
rwlockes_where.insert({ rwlock, { i } });
}
else {
it->second++;
rwlockes_where.find(rwlock)->second.push_back(i);
}
}
else if (data[i].la == a_rw_unlock) {
// here it is not important if it is the r or
// the w lock, as long as the count matches up
auto it = rwlocks_counts.find(rwlock);
if (it != rwlocks_counts.end()) {
if (it->second > 0)
it->second--;
if (it->second == 0) {
rwlocks_counts.erase(rwlock);
rwlockes_where.erase(rwlock);
}
}
}
}
return rwlockes_where;
}
void find_still_locked_rwlock(FILE *const fh, const lock_trace_item_t *const data, const uint64_t n_records)
{
auto still_locked_list = do_find_still_locked_rwlock(data, n_records);
fprintf(fh, "<section>\n");
fprintf(fh, "<h2 id=\"stillrw\">7. still locked rwlocks</h2>\n");
fprintf(fh, "<p>A list of the r/w-locks that were still locked when the program terminated.</p>\n");
fprintf(fh, "<p>Count: %zu</p>\n", still_locked_list.size());
for(auto it : still_locked_list) {
fprintf(fh, "<h3>rwlock %p</h3>\n", (const void *)it.first);
auto unique_backtraces = find_a_record_for_unique_backtrace_hashes(data, it.second);
if (unique_backtraces.size() == 1)
fprintf(fh, "<p>The following location did not unlock:</p>\n");
else
fprintf(fh, "<p>One of the following locations did not unlock:</p>\n");
for(auto entry : unique_backtraces) {
put_record_details_html(fh, data[entry.second], "magenta");
fprintf(fh, "<br>\n");
}
}
fprintf(fh, "</section>\n");
}
// see do_find_double_un_locks_mutex comment about false positives
auto do_find_double_un_locks_rwlock(const lock_trace_item_t *const data, const size_t n_records)
{
std::map<std::pair<const pthread_rwlock_t *, lock_action_error_t>, std::map<hash_t, double_un_lock_t> > out;
std::map<const pthread_rwlock_t *, std::set<pid_t> > r_locked;
std::map<const pthread_rwlock_t *, std::set<pid_t> > w_locked;
for(size_t i=0; i<n_records; i++) {
// ignore calls that failed
if (data[i].rc != 0)
continue;
const pthread_rwlock_t *const rwlock = (const pthread_rwlock_t *)data[i].lock;
const pid_t tid = data[i].tid;
if (data[i].la == a_r_lock) {
// see if it is already locked by current 'tid' which is a mistake
auto it = r_locked.find(rwlock);
if (it != r_locked.end()) {
if (it->second.find(tid) != it->second.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, rwlock, lae_already_locked, hash, i);
}
else {
// new locker of this rwlock
it->second.insert(tid);
}
}
else {
// new rwlock
r_locked.insert({ rwlock, { tid } });
}
}
else if (data[i].la == a_w_lock) {
auto it = w_locked.find(rwlock);
if (it != w_locked.end()) {
if (it->second.find(tid) != it->second.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, rwlock, lae_already_locked, hash, i);
}
else {
it->second.insert(tid);
}
}
else {
w_locked.insert({ rwlock, { tid } });
}
}
else if (data[i].la == a_rw_unlock) {
// see if it is not locked (mistake)
auto w_it = w_locked.find(rwlock);
if (w_it == w_locked.end()) {
// check r_locked
auto r_it = r_locked.find(rwlock);
if (r_it == r_locked.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, rwlock, lae_not_locked, hash, i);
}
// see if it is not locked by current tid (mistake)
else {
auto tid_it = r_it->second.find(tid);
if (tid_it == r_it->second.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, rwlock, lae_not_owner, hash, i);
}
else {
r_it->second.erase(tid_it);
}
if (r_it->second.empty())
r_locked.erase(r_it);
}
}
// see if it is not locked by current tid (mistake)
// that is: not locked or waiting to acquire the w-lock
else {
auto tid_it = w_it->second.find(tid);
if (tid_it == w_it->second.end()) {
hash_t hash = calculate_backtrace_hash(data[i].caller, CALLER_DEPTH);
put_lock_error(&out, rwlock, lae_not_owner, hash, i);
}
else {
w_it->second.erase(tid_it);
}
if (w_it->second.empty())
w_locked.erase(w_it);
}
}
}
return out;
}
void find_double_un_locks_rwlock(FILE *const fh, const lock_trace_item_t *const data, const uint64_t n_records)
{
auto rw_lock_mistakes = do_find_double_un_locks_rwlock(data, n_records);
fprintf(fh, "<section>\n");
fprintf(fh, "<h2 id=\"doublerw\">6. r/w-lock lock/unlock mistakes</h2>\n");
fprintf(fh, "<p>Mistakes are: read-locking a r/w-lock another time by the same thread, unlocking r/w-locks that are not locked and unlocking of an r/w-lock by some other thread than the one who locked it.</p>\n");
fprintf(fh, "<p>This section contains a list of all the seen r/w-lock/error-type combinations and then for each the mistakes made and then one or more backtraces (\"first\" and \"next\") where they occured.</p>\n");
fprintf(fh, "<p>Count: %zu</p>\n", rw_lock_mistakes.size());
// go through all mutexes for which a mistake was made
for(auto rwlock_lock_mistake : rw_lock_mistakes) {
fprintf(fh, "<h3>r/w-lock %p, type \"%s\"</h3>\n", (const void *)rwlock_lock_mistake.first.first, lock_action_error_str[rwlock_lock_mistake.first.second]);
// go through every combination (lock + unlocks)
for(auto map_entry : rwlock_lock_mistake.second) {
double_un_lock_t & dul = map_entry.second;
// first (correct?)
if (dul.latest_records.empty() == false)
fprintf(fh, "<h4>first</h4>\n");
put_record_details_html(fh, data[dul.first_record], "yellow");
// then list all mistakes for this combination, show only unique backtraces
if (dul.latest_records.empty() == false) {
fprintf(fh, "<h4>next</h4>\n");
fprintf(fh, "<p>Mistake count: %zu (total number of backtraces seen; note that the list below is de-duplicated).</p>\n", dul.latest_records.size());
auto unique_backtraces = find_a_record_for_unique_backtrace_hashes(data, dul.latest_records);
for(auto entry : unique_backtraces)
put_record_details_html(fh, data[entry.second], "yellow");
}
fprintf(fh, "<br>\n");
}
}
fprintf(fh, "</section>\n");
}
void put_html_header(FILE *const fh, const bool run_correlate)
{
fprintf(fh, "<!DOCTYPE html>\n<html lang=\"en\"><head>\n");
fprintf(fh, "<meta charset=\"utf-8\">\n");
fprintf(fh, "<style>.svgbox{height:768px;width:1024px;overflow:scroll}thead th{ background: #ffb0b0}table{font-size:16px;border-collapse:collapse;border-spacing:0;}td,th{border:1px solid #ddd;text-align:left;padding:8px}tr:nth-child(even){background-color:#f2f2f2}.green{background-color:#c0ffc0}.red{background-color:#ffc0c0}.blue{background-color:#c0c0ff}.yellow{background-color:#ffffa0}.magenta{background-color:#ffa0ff}th{padding-top:11px;padding-bottom:11px;background-color:#04aa6d;color:#fff}h1,h2,h3{margin-top:2.2em;}</style>\n");
fprintf(fh, "<title>lock trace</title></head><body>\n");
fprintf(fh, "<h1>LOCK TRACE</h1>\n");
fprintf(fh, "<h2>table of contents</h2>\n");
fprintf(fh, "<p>Please note: the colors are only used for easier reading, they don't have a special meaning.</p>\n");
fprintf(fh, "<ol>\n");
fprintf(fh, "<li><a href=\"#meta\">meta data</a>\n");
fprintf(fh, "<li><a href=\"#durations\">durations</a>\n");
fprintf(fh, "<li><a class=\"green\" href=\"#errors\">errors</a>\n");
fprintf(fh, "<li><a class=\"red\" href=\"#doublem\">double lock/unlock mutexes</a>\n");
fprintf(fh, "<li><a class=\"blue\" href=\"#stillm\">still locked mutexes</a>\n");
fprintf(fh, "<li><a class=\"yellow\" href=\"#doublerw\">double lock/unlock r/w-locks</a>\n");
fprintf(fh, "<li><a class=\"magenta\" href=\"#stillrw\">still locked r/w-locks</a>\n");
fprintf(fh, "<li><a class=\"green\" href=\"#whereused\">where are locks used</a>\n");
if (run_correlate)
fprintf(fh, "<li><a href=\"#corr\">correlations between locks</a>\n");
fprintf(fh, "</ol>\n");
fprintf(fh, "<p>The \"tid\" is the thread identifier of the thread that triggered a measurement.</p>\n");
}
void put_html_tail(FILE *const fh)
{
const char footer[] = "<p><br><br></p><hr><footer>This <b>locktracer</b> is (C) 2021 by Folkert van Heusden <[email protected]></footer></body></html>\n";
if (fprintf(fh, "<p><br><br></p><hr><footer>This <b>locktracer</b> is (C) 2021 by Folkert van Heusden <[email protected]></footer></body></html>\n") != sizeof footer - 1)
fprintf(stderr, "Problem writing output-file: filesystem full?\n");
}
std::string get_json_string(const json_t *const js, const char *const key)
{
return json_string_value(json_object_get(js, key));
}
int64_t get_json_int(const json_t *const js, const char *const key)
{
return json_integer_value(json_object_get(js, key));
}
std::map<std::string, uint64_t> data_stats(const lock_trace_item_t *const data, const uint64_t n_records)
{
uint64_t cnts[_a_max][2] { { 0, 0 } };
for(uint64_t i=0; i<n_records; i++)
cnts[data[i].la][!!data[i].rc]++;
std::map<std::string, uint64_t> out;
out.insert({ "mutex locks", cnts[a_lock][0] });
out.insert({ "mutex unlocks", cnts[a_unlock][0] });
out.insert({ "pthread_clean", cnts[a_thread_clean][0] });
out.insert({ "rw read lock", cnts[a_r_lock][0] });
out.insert({ "rw write lock", cnts[a_w_lock][0] });
out.insert({ "rw unlock", cnts[a_rw_unlock][0] });
out.insert({ "mutex init", cnts[a_init][0] });
out.insert({ "mutex destroy", cnts[a_destroy][0] });
out.insert({ "rw init", cnts[a_rw_init][0] });
out.insert({ "rw destroy", cnts[a_rw_destroy][0] });
out.insert({ "failed mutex locks", cnts[a_lock][1] });
out.insert({ "failed mutex unlocks", cnts[a_unlock][1] });
out.insert({ "failed pthread_clean", cnts[a_thread_clean][1] });
out.insert({ "failed rw read lock", cnts[a_r_lock][1] });
out.insert({ "failed rw write lock", cnts[a_w_lock][1] });
out.insert({ "failed rw unlock", cnts[a_rw_unlock][1] });
out.insert({ "failed mutex init", cnts[a_init][1] });
out.insert({ "failed mutex destroy", cnts[a_destroy][1] });
out.insert({ "failed rw init", cnts[a_rw_init][1] });
out.insert({ "failed rw destroy", cnts[a_rw_destroy][1] });
return out;
}
void emit_meta_data(FILE *const fh, const json_t *const meta, const std::string & core_file_in, const std::string & trace_file, const lock_trace_item_t *const data, const uint64_t n_records)
{
fprintf(fh, "<h2 id=\"meta\">1. META DATA</h2>\n");
fprintf(fh, "<table><tr><th colspan=2>meta data</th></tr>\n");
fprintf(fh, "<tr><th>executable</th><td>%s</td></tr>\n", get_json_string(meta, "exe_name").c_str());
fprintf(fh, "<tr><th>PID</th><td>%ld</td></tr>\n", get_json_int(meta, "pid"));
fprintf(fh, "<tr><th>scheduler</th><td>%s</td></tr>\n", get_json_string(meta, "scheduler").c_str());
fprintf(fh, "<tr><th>host name</th><td>%s</td></tr>\n", get_json_string(meta, "hostname").c_str());
fprintf(fh, "<tr><th>core file</th><td>%s</td></tr>\n", core_file_in.c_str());
fprintf(fh, "<tr><th>trace file</th><td>%s</td></tr>\n", trace_file.c_str());
double took = double(get_json_int(meta, "end_ts") - get_json_int(meta, "start_ts")) / billion;
uint64_t _n_records = get_json_int(meta, "n_records");
uint64_t _n_records_max = get_json_int(meta, "n_records_max");
double n_per_sec = took > 0 ? _n_records / took: 0;
fprintf(fh, "<tr><th># trace records</th><td>%lu (%.2f%%, %.2f%%/s)</td></tr>\n", _n_records, _n_records * 100.0 / _n_records_max, n_per_sec * 100.0 / _n_records_max);
fprintf(fh, "<tr><th>fork warning</th><td>%s</td></tr>\n", get_json_int(meta, "fork_warning") ? "true" : "false");
fprintf(fh, "<tr><th># cores</th><td>%ld</td></tr>\n", get_json_int(meta, "n_procs"));
uint64_t start_ts = get_json_int(meta, "start_ts");
uint64_t end_ts = get_json_int(meta, "end_ts");
fprintf(fh, "<tr><th>started at</th><td>%.9f (%s)</td></tr>\n", start_ts / double(billion), my_ctime(start_ts).c_str());
fprintf(fh, "<tr><th>stopped at</th><td>%.9f (%s)</td></tr>\n", end_ts / double(billion), my_ctime(end_ts).c_str());
fprintf(fh, "<tr><th>took</th><td>%fs</td></tr>\n", took);
fprintf(fh, "</table>\n");
fprintf(fh, "<h3>counts</h3>\n");
fprintf(fh, "<table>\n");
fprintf(fh, "<tr><th># mutex try-locks</th><td>%ld</td></tr>\n", get_json_int(meta, "cnt_mutex_trylock"));
fprintf(fh, "<tr><th># rwlock try-rdlock</th><td>%ld</td></tr>\n", get_json_int(meta, "cnt_rwlock_try_rdlock"));
fprintf(fh, "<tr><th># rwlock try-timed-rdlock</th><td>%ld</td></tr>\n", get_json_int(meta, "cnt_rwlock_try_timedrdlock"));
fprintf(fh, "<tr><th># rwlock try-wrlock</th><td>%ld</td></tr>\n", get_json_int(meta, "cnt_rwlock_try_wrlock"));
fprintf(fh, "<tr><th># rwlock try-timed-rwlock</th><td>%ld</td></tr>\n", get_json_int(meta, "cnt_rwlock_try_timedwrlock"));
assert(n_records == _n_records);
auto ds = data_stats(data, n_records);
for(auto ds_entry : ds)
fprintf(fh, "<tr><th>%s</th><td>%lu</td></tr>\n", ds_entry.first.c_str(), ds_entry.second);
fprintf(fh, "</table>\n");
}
typedef struct {
uint64_t mutex_lock_acquire_durations, n_mutex_acquire_locks, mutex_lock_acquire_sd, mutex_lock_acquire_max;
} durations_mutex_t;
typedef struct {
uint64_t mutex_locked_durations, n_mutex_locked_durations, mutex_locked_durations_sd, mutex_locked_durations_max;
} locked_durations_mutex_t;
typedef struct {
uint64_t rwlock_r_lock_acquire_durations, n_rwlock_r_acquire_locks, rwlock_r_lock_acquire_sd, rwlock_r_lock_acquire_max;
} durations_rwlock_r_t;
typedef struct {
uint64_t rwlock_w_lock_acquire_durations, n_rwlock_w_acquire_locks, rwlock_w_lock_acquire_sd, rwlock_w_lock_acquire_max;
} durations_rwlock_w_t;
typedef struct {
uint64_t rwlock_r_locked_durations, n_rwlock_r_locked, rwlock_r_locked_sd, rwlock_r_locked_max;
uint64_t rwlock_w_locked_durations, n_rwlock_w_locked, rwlock_w_locked_sd, rwlock_w_locked_max;
} locked_durations_rwlock_t;
typedef struct {
// acquire
durations_mutex_t durations_mutex;
std::map<pthread_mutex_t *, durations_mutex_t> per_mutex_durations;
// hold
locked_durations_mutex_t locked_durations;
std::map<pthread_mutex_t *, locked_durations_mutex_t> per_mutex_locked_durations;
// acquire
durations_rwlock_r_t durations_r_rwlock;
durations_rwlock_w_t durations_w_rwlock;
std::map<pthread_rwlock_t *, durations_rwlock_r_t> per_rwlock_r_acquire_durations;
std::map<pthread_rwlock_t *, durations_rwlock_w_t> per_rwlock_w_acquire_durations;
// hold
std::map<pthread_rwlock_t *, locked_durations_rwlock_t> per_rwlock_locked_durations;
} durations_t;