Repository navigation
Expand file tree
/
Copy pathSTAT_BackEnd.C
More file actions
4244 lines (3841 loc) · 160 KB
/
Copy pathSTAT_BackEnd.C
File metadata and controls
4244 lines (3841 loc) · 160 KB
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
/**
Copyright (c) 2007-2020, Lawrence Livermore National Security, LLC.
Produced at the Lawrence Livermore National Laboratory
Written by Gregory Lee [lee218@llnl.gov], Dorian Arnold, Matthew LeGendre, Dong Ahn, Bronis de Supinski, Barton Miller, Martin Schulz, Niklas Nielson, Nicklas Bo Jensen, Jesper Nielson, and Sven Karlsson.
LLNL-CODE-750488.
All rights reserved.
This file is part of STAT. For details, see http://www.github.com/LLNL/STAT. Please also read STAT/LICENSE.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the disclaimer below.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the disclaimer (as noted below) in the documentation and/or other materials provided with the distribution.
Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "STAT_BackEnd.h"
using namespace std;
using namespace MRN;
using namespace Dyninst;
using namespace Dyninst::Stackwalker;
using namespace Dyninst::SymtabAPI;
#ifdef SW_VERSION_8_0_0
using namespace Dyninst::ProcControlAPI;
#endif
#ifdef STAT_FGFS
using namespace FastGlobalFileStatus;
using namespace FastGlobalFileStatus::MountPointAttribute;
using namespace FastGlobalFileStatus::CommLayer;
#endif
//! the STAT log handle, global for filter access
FILE *gStatOutFp = NULL;
//! a pointer to the current STAT_BackEnd object
STAT_BackEnd *gBePtr;
extern const char *gNodeAttrs[];
extern const char *gEdgeAttrs[];
extern int gNumNodeAttrs;
extern int gNumEdgeAttrs;
STAT_BackEnd::STAT_BackEnd(StatDaemonLaunch_t launchType) :
swLogBuffer_(STAT_SW_DEBUG_BUFFER_LENGTH),
launchType_(launchType)
{
gStatOutFp = NULL;
proctabSize_ = 0;
processMapNonNull_ = 0;
logType_ = 0;
parentHostName_ = NULL;
swDebugFile_ = NULL;
#ifdef BGL
errOutFp_ = stdout;
#else
errOutFp_ = stderr;
#endif
initialized_ = false;
connected_ = false;
isRunning_ = false;
#ifdef GROUP_OPS
doGroupOps_ = false;
#endif
network_ = NULL;
myRank_ = 0;
parentRank_ = 0;
parentPort_ = 0;
nDaemonsPerNode_ = 1;
myNodeRank_ = 0;
broadcastStream_ = NULL;
proctab_ = NULL;
sampleType_ = 0;
nVariables_ = 0;
extractVariables_ = NULL;
snprintf(outDir_, BUFSIZE, "NULL");
snprintf(filePrefix_, BUFSIZE, "NULL");
#ifdef STAT_FGFS
fgfsCommFabric_ = NULL;
#endif
#ifdef DYSECTAPI
dysectBE_ = NULL;
#endif
gBePtr = this;
registerSignalHandlers(true);
}
STAT_BackEnd::~STAT_BackEnd()
{
int i;
graphlib_error_t graphlibError;
map<int, StatBitVectorEdge_t *>::iterator nodeInEdgeMapIter;
clear2dNodesAndEdges();
clear3dNodesAndEdges();
if (parentHostName_ != NULL)
{
free(parentHostName_);
parentHostName_ = NULL;
}
if (extractVariables_ != NULL)
{
free(extractVariables_);
extractVariables_ = NULL;
}
if (proctab_ != NULL)
{
for (i = 0; i < proctabSize_; i++)
{
if (proctab_[i].executable_name != NULL)
free(proctab_[i].executable_name);
if (proctab_[i].host_name != NULL)
free(proctab_[i].host_name);
}
free(proctab_);
proctab_ = NULL;
}
graphlibError = graphlib_Finish();
if (GRL_IS_FATALERROR(graphlibError))
fprintf(errOutFp_, "Warning: Failed to finish graphlib\n");
statFreeReorderFunctions();
statFreeBitVectorFunctions();
statFreeCountRepFunctions();
statFreeMergeFunctions();
#ifdef STAT_FGFS
if (fgfsCommFabric_ != NULL)
{
delete fgfsCommFabric_;
fgfsCommFabric_ = NULL;
}
#endif
/* clean up MRNet */
if (network_ != NULL)
{
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "waiting for shutdown\n");
network_->waitfor_ShutDown();
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "deleting network\n");
delete network_;
network_ = NULL;
}
#ifdef DYSECTAPI
dysectBE_ = NULL;
#endif
registerSignalHandlers(false);
gBePtr = NULL;
usingGdb_ = false;
}
void STAT_BackEnd::clear2dNodesAndEdges()
{
map<int, pair<int, StatBitVectorEdge_t *> >::iterator edgesIter;
nodes2d_.clear();
for (edgesIter = edges2d_.begin(); edgesIter != edges2d_.end(); edgesIter++)
if (edgesIter->second.second != NULL)
statFreeEdge(edgesIter->second.second);
edges2d_.clear();
map<int, map<string, void *> >::iterator edgeIdToAttrsIter;
map<string, void *>::iterator edgeAttrsIter;
for (edgeIdToAttrsIter = edgeIdToAttrs_.begin(); edgeIdToAttrsIter != edgeIdToAttrs_.end(); edgeIdToAttrsIter++)
for (edgeAttrsIter = edgeIdToAttrsIter->second.begin(); edgeAttrsIter != edgeIdToAttrsIter->second.end(); edgeAttrsIter++)
statFreeEdgeAttr(edgeAttrsIter->first.c_str(), edgeAttrsIter->second);
edgeIdToAttrs_.clear();
}
void STAT_BackEnd::clear3dNodesAndEdges()
{
map<int, pair<int, StatBitVectorEdge_t *> >::iterator edgesIter;
nodes3d_.clear();
for (edgesIter = edges3d_.begin(); edgesIter != edges3d_.end(); edgesIter++)
if (edgesIter->second.second != NULL)
statFreeEdge(edgesIter->second.second);
edges3d_.clear();
nodeIdToAttrs_.clear();
map<int, map<string, void *> >::iterator edgeIdToAttrsIter;
map<string, void *>::iterator edgeAttrsIter;
for (edgeIdToAttrsIter = edgeIdToAttrs3d_.begin(); edgeIdToAttrsIter != edgeIdToAttrs3d_.end(); edgeIdToAttrsIter++)
for (edgeAttrsIter = edgeIdToAttrsIter->second.begin(); edgeAttrsIter != edgeIdToAttrsIter->second.end(); edgeAttrsIter++)
statFreeEdgeAttr(edgeAttrsIter->first.c_str(), edgeAttrsIter->second);
edgeIdToAttrs3d_.clear();
}
StatError_t STAT_BackEnd::update3dNodesAndEdges()
{
map<int, string>::iterator nodesIter;
map<int, pair<int, StatBitVectorEdge_t *> >::iterator edgesIter;
StatBitVectorEdge_t *edge;
for (nodesIter = nodes2d_.begin(); nodesIter != nodes2d_.end(); nodesIter++)
nodes3d_[nodesIter->first] = nodesIter->second;
for (edgesIter = edges2d_.begin(); edgesIter != edges2d_.end(); edgesIter++)
{
if (edges3d_.find(edgesIter->first) == edges3d_.end())
{
edge = initializeBitVectorEdge(proctabSize_);
if (edge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to initialize edge\n");
return STAT_ALLOCATE_ERROR;
}
edges3d_[edgesIter->first] = make_pair(edgesIter->second.first, edge);
}
statMergeEdge(edges3d_[edgesIter->first].second, edgesIter->second.second);
int i, dst;
StatCountRepEdge_t *countRepEdge = NULL;
dst = edgesIter->first;
if (edgeIdToAttrs3d_.find(dst) == edgeIdToAttrs3d_.end())
{
map<string, void *> edgeAttrs;
for (i = 0; i < gNumEdgeAttrs; i++)
edgeAttrs[gEdgeAttrs[i]] = NULL;
edgeIdToAttrs3d_[edgesIter->first] = edgeAttrs;
}
for (i = 0; i < gNumEdgeAttrs; i++)
edgeIdToAttrs3d_[dst][gEdgeAttrs[i]] = statMergeEdgeAttr(gEdgeAttrs[i], edgeIdToAttrs3d_[dst][gEdgeAttrs[i]], edgeIdToAttrs_[dst][gEdgeAttrs[i]]);
if (sampleType_ & STAT_SAMPLE_COUNT_REP)
{
countRepEdge = getBitVectorCountRep((StatBitVectorEdge_t *)edgeIdToAttrs3d_[dst]["bv"], statRelativeRankToAbsoluteRank);
if (countRepEdge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to translate bit vector into count + representative\n");
return STAT_ALLOCATE_ERROR;
}
statFreeEdgeAttr("count", edgeIdToAttrs3d_[dst]["count"]);
statFreeEdgeAttr("rep", edgeIdToAttrs3d_[dst]["rep"]);
edgeIdToAttrs3d_[dst]["count"] = statCopyEdgeAttr("count", (void *)&countRepEdge->count);
edgeIdToAttrs3d_[dst]["rep"] = statCopyEdgeAttr("rep" , (void *)&countRepEdge->representative);
edgeIdToAttrs_[dst]["sum"] = statMergeEdgeAttr("sum", edgeIdToAttrs_[dst]["sum"], (void *)&countRepEdge->checksum);
statFreeCountRepEdge(countRepEdge);
}
if (sampleType_ & STAT_SAMPLE_THREADS)
{
countRepEdge = getBitVectorCountRep((StatBitVectorEdge_t *)edgeIdToAttrs3d_[dst]["tbv"], statDummyRelativeRankToAbsoluteRank);
if (countRepEdge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to translate bit vector into count + representative\n");
return STAT_ALLOCATE_ERROR;
}
statFreeEdgeAttr("tcount", edgeIdToAttrs3d_[dst]["tcount"]);
edgeIdToAttrs3d_[dst]["tcount"] = statCopyEdgeAttr("tcount", (void *)&countRepEdge->count);
statFreeEdgeAttr("sum", edgeIdToAttrs3d_[dst]["sum"]);
edgeIdToAttrs3d_[dst]["sum"] = statCopyEdgeAttr("sum", (void *)&countRepEdge->count);
statFreeEdgeAttr("tbvsum", edgeIdToAttrs3d_[dst]["tbvsum"]);
// we need a unique checksum for threads across damons:
int64_t tbvsum = countRepEdge->checksum * (myRank_ + 1);
edgeIdToAttrs3d_[dst]["tbvsum"] = statCopyEdgeAttr("tbvsum", (void *)&tbvsum);
statFreeCountRepEdge(countRepEdge);
}
} // for edgesIter
return STAT_OK;
}
StatError_t STAT_BackEnd::update2dEdge(int src, int dst, StatBitVectorEdge_t *edge, Dyninst::THR_ID tid)
{
StatBitVectorEdge_t *newEdge = NULL;
if (edges2d_.find(dst) == edges2d_.end())
{
newEdge = initializeBitVectorEdge(proctabSize_);
if (newEdge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to initialize newEdge\n");
return STAT_ALLOCATE_ERROR;
}
edges2d_[dst] = make_pair(src, newEdge);
}
statMergeEdge(edges2d_[dst].second, edge);
int i, tidIndex;
map<string, void *> edgeAttrs;
StatCountRepEdge_t *countRepEdge = NULL;
if (edgeIdToAttrs_.find(dst) == edgeIdToAttrs_.end())
{
for (i = 0; i < gNumEdgeAttrs; i++)
edgeAttrs[gEdgeAttrs[i]] = NULL;
edgeIdToAttrs_[dst] = edgeAttrs;
}
// we always maintain a bit vector and just won't send it if sampling count + rep
statFreeEdgeAttr("bv", edgeIdToAttrs_[dst]["bv"]);
edgeIdToAttrs_[dst]["bv"] = statCopyEdgeAttr("bv", (void *)edges2d_[dst].second);
if (sampleType_ & STAT_SAMPLE_THREADS)
{
newEdge = initializeBitVectorEdge(threadBvLength_);
if (newEdge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to initialize newEdge\n");
return STAT_ALLOCATE_ERROR;
}
tidIndex = find(threadIds_.begin(), threadIds_.end(), tid) - threadIds_.begin();
tidIndex = tidIndex % threadBvLength_;
newEdge->bitVector[tidIndex / STAT_BITVECTOR_BITS] |= STAT_GRAPH_BIT(tidIndex % STAT_BITVECTOR_BITS);
edgeIdToAttrs_[dst]["tbv"] = statMergeEdgeAttr("tbv", edgeIdToAttrs_[dst]["tbv"], (void *)newEdge);
statFreeEdge(newEdge);
countRepEdge = getBitVectorCountRep((StatBitVectorEdge_t *)edgeIdToAttrs_[dst]["tbv"], statDummyRelativeRankToAbsoluteRank);
if (countRepEdge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to translate bit vector into count + representative\n");
return STAT_ALLOCATE_ERROR;
}
statFreeEdgeAttr("tcount", edgeIdToAttrs_[dst]["tcount"]);
edgeIdToAttrs_[dst]["tcount"] = statCopyEdgeAttr("tcount", (void *)&countRepEdge->count);
edgeIdToAttrs_[dst]["sum"] = statMergeEdgeAttr("sum", edgeIdToAttrs_[dst]["sum"], (void *)&countRepEdge->count);
// we need a unique checksum for threads across damons:
int64_t tbvsum = countRepEdge->checksum * (myRank_ + 1);
edgeIdToAttrs_[dst]["tbvsum"] = statMergeEdgeAttr("tbvsum", edgeIdToAttrs_[dst]["tbvsum"], (void *)&tbvsum);
statFreeCountRepEdge(countRepEdge);
} //if (sampleType_ & STAT_SAMPLE_THREADS)
if (sampleType_ & STAT_SAMPLE_COUNT_REP)
{
countRepEdge = getBitVectorCountRep(edges2d_[dst].second, statRelativeRankToAbsoluteRank);
if (countRepEdge == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "Failed to translate bit vector into count + representative\n");
return STAT_ALLOCATE_ERROR;
}
statFreeEdgeAttr("count", edgeIdToAttrs_[dst]["count"]);
statFreeEdgeAttr("rep", edgeIdToAttrs_[dst]["rep"]);
edgeIdToAttrs_[dst]["count"] = statCopyEdgeAttr("count", (void *)&countRepEdge->count);
edgeIdToAttrs_[dst]["rep"] = statCopyEdgeAttr("rep" , (void *)&countRepEdge->representative);
edgeIdToAttrs_[dst]["sum"] = statMergeEdgeAttr("sum", edgeIdToAttrs_[dst]["sum"], (void *)&countRepEdge->checksum);
statFreeCountRepEdge(countRepEdge);
}
return STAT_OK;
}
StatError_t STAT_BackEnd::generateGraphs(graphlib_graph_p *prefixTree2d, graphlib_graph_p *prefixTree3d)
{
int i;
string::size_type delimPos;
string tempString;
graphlib_graph_p *currentGraph;
graphlib_error_t graphlibError;
graphlib_nodeattr_t nodeAttr = {1,0,20,GRC_LIGHTGREY,0,0,(char *)"",-1,NULL};
graphlib_edgeattr_t edgeAttr = {1,0,NULL,0,0,0,NULL};
map<int, string> *nodes;
map<int, pair<int, StatBitVectorEdge_t *> > *edges;
map<int, string>::iterator nodesIter;
map<int, pair<int, StatBitVectorEdge_t *> >::iterator edgesIter;
for (i = 0; i < 2; i++)
{
if (i == 0)
{
currentGraph = prefixTree2d;
nodes = &nodes2d_;
edges = &edges2d_;
}
else
{
currentGraph = prefixTree3d;
nodes = &nodes3d_;
edges = &edges3d_;
}
/* Delete previously merged graphs */
if (*currentGraph != NULL)
{
graphlibError = graphlib_delGraph(*currentGraph);
if (GRL_IS_FATALERROR(graphlibError))
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Error deleting graph\n");
}
*currentGraph = createRootedGraph(sampleType_);
if (*currentGraph == NULL)
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Error initializing graph\n");
return STAT_GRAPHLIB_ERROR;
}
for (nodesIter = (*nodes).begin(); nodesIter != (*nodes).end(); nodesIter++)
{
int index;
map<string, string>::iterator nodeAttrIter;
nodeAttr.attr_values = (void **)calloc(1, gNumNodeAttrs * sizeof(void *));
if (nodeAttr.attr_values == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s: Error callocing %d nodeAttr.attr_values\n", strerror(errno), gNumNodeAttrs);
return STAT_ALLOCATE_ERROR;
}
for (nodeAttrIter = nodeIdToAttrs_[nodesIter->first].begin(); nodeAttrIter != nodeIdToAttrs_[nodesIter->first].end(); nodeAttrIter++)
{
graphlibError = graphlib_getNodeAttrIndex(*currentGraph, nodeAttrIter->first.c_str(), &index);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Failed to get node attr index for key %s\n", nodeAttrIter->first.c_str());
free(nodeAttr.attr_values);
return STAT_GRAPHLIB_ERROR;
}
nodeAttr.attr_values[index] = statCopyNodeAttr(nodeAttrIter->first.c_str(), nodeAttrIter->second.c_str());
}
graphlibError = graphlib_addNode(*currentGraph, nodesIter->first, &nodeAttr);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Error adding node to graph\n");
free(nodeAttr.attr_values);
return STAT_GRAPHLIB_ERROR;
}
statFreeNodeAttrs(nodeAttr.attr_values, *currentGraph);
}
for (edgesIter = (*edges).begin(); edgesIter != (*edges).end(); edgesIter++)
{
int index;
edgeAttr.attr_values = (void **)calloc(1, gNumEdgeAttrs * sizeof(void *));
if (edgeAttr.attr_values == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s: Error callocing %d edgeAttr.attr_values\n", strerror(errno), gNumEdgeAttrs);
return STAT_ALLOCATE_ERROR;
}
map<string, void *>::iterator edgeAttrIter, edgeAttrIterEnd;
if (currentGraph == prefixTree2d)
{
edgeAttrIter = edgeIdToAttrs_[edgesIter->first].begin();
edgeAttrIterEnd = edgeIdToAttrs_[edgesIter->first].end();
}
else
{
edgeAttrIter = edgeIdToAttrs3d_[edgesIter->first].begin();
edgeAttrIterEnd = edgeIdToAttrs3d_[edgesIter->first].end();
}
while (edgeAttrIter != edgeAttrIterEnd)
{
graphlibError = graphlib_getEdgeAttrIndex(*currentGraph, edgeAttrIter->first.c_str(), &index);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Failed to get edge attr index for key %s\n", edgeAttrIter->first.c_str());
free(edgeAttr.attr_values);
return STAT_GRAPHLIB_ERROR;
}
if ((sampleType_ & STAT_SAMPLE_COUNT_REP && strcmp(edgeAttrIter->first.c_str(), "bv") == 0) || strcmp(edgeAttrIter->first.c_str(), "tbv") == 0)
{
edgeAttrIter++;
continue;
}
edgeAttr.attr_values[index] = statCopyEdgeAttr(edgeAttrIter->first.c_str(), edgeAttrIter->second);
edgeAttrIter++;
}
graphlibError = graphlib_addDirectedEdge(*currentGraph, edgesIter->second.first, edgesIter->first, &edgeAttr);
if (GRL_IS_FATALERROR(graphlibError))
{
statFreeEdgeAttrs(edgeAttr.attr_values, *currentGraph);
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Error adding edge to graph\n");
return STAT_GRAPHLIB_ERROR;
}
statFreeEdgeAttrs(edgeAttr.attr_values, *currentGraph);
}
}
return STAT_OK;
}
StatError_t STAT_BackEnd::init(int *argc, char ***argv)
{
/* unblock all signals. */
/* In 3/2014, SIGUSR2 was found to be blocked on Cray systems, which was causing detach to hang */
sigset_t mask;
int intRet = sigfillset(&mask);
if (intRet == 0)
{
intRet = pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
if (intRet != 0)
fprintf(stderr, "warning: failed to set pthread_sigmask: %s\n", strerror(errno));
}
char abuf[INET_ADDRSTRLEN], fileName[BUFSIZE], *envValue;
FILE *file;
struct sockaddr_in *sinp = NULL;
struct addrinfo *addinf = NULL;
graphlib_error_t graphlibError;
/* Get hostname and IP address */
intRet = gethostname(localHostName_, BUFSIZE);
if (intRet != 0)
{
fprintf(errOutFp_, "Error: gethostname returned %d\n", intRet);
return STAT_SYSTEM_ERROR;
}
intRet = getaddrinfo(localHostName_, NULL, NULL, &addinf);
if (intRet != 0)
fprintf(errOutFp_, "Warning: getaddrinfo returned %d\n", intRet);
if (addinf != NULL)
sinp = (struct sockaddr_in *)addinf->ai_addr;
if (sinp != NULL)
snprintf(localIp_, BUFSIZE, "%s", inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN));
if (addinf != NULL)
freeaddrinfo(addinf);
graphlibError = graphlib_Init();
if (GRL_IS_FATALERROR(graphlibError))
{
fprintf(errOutFp_, "Failed to initialize Graphlib\n");
return STAT_GRAPHLIB_ERROR;
}
statInitializeReorderFunctions();
statInitializeBitVectorFunctions();
statInitializeMergeFunctions();
statInitializeCountRepFunctions();
/* Enable MRNet logging */
envValue = getenv("STAT_MRNET_OUTPUT_LEVEL");
if (envValue != NULL)
set_OutputLevel(atoi(envValue));
envValue = getenv("STAT_MRNET_DEBUG_LOG_DIRECTORY");
if (envValue != NULL)
{
intRet = setenv("MRNET_DEBUG_LOG_DIRECTORY", envValue, 1);
if (intRet != 0)
fprintf(errOutFp_, "Warning: %s: setenv returned %d\n", strerror(errno), intRet);
}
/* Redirect output to a file */
envValue = getenv("STAT_OUTPUT_REDIRECT_DIR");
if (envValue != NULL)
{
snprintf(fileName, BUFSIZE, "%s/%s.stdout.txt", envValue, localHostName_);
file = freopen(fileName, "w", stdout);
if (file == NULL)
fprintf(errOutFp_, "Warning: %s: frepoen returned NULL for stdout to file %s\n", strerror(errno), fileName);
snprintf(fileName, BUFSIZE, "%s/%s.stderr.txt", envValue, localHostName_);
file = freopen(fileName, "w", stderr);
if (file == NULL)
fprintf(errOutFp_, "Warning: %s: frepoen returned NULL for stdwerr to file %s\n", strerror(errno), fileName);
}
#ifdef BGL
doGroupOps_ = true;
#else
envValue = getenv("STAT_GROUP_OPS");
doGroupOps_ = (envValue != NULL);
#endif
return STAT_OK;
}
static void onCrashWrap(int sig, siginfo_t *info, void *context)
{
gBePtr->onCrash(sig, info, context);
}
void STAT_BackEnd::onCrash(int sig, siginfo_t *, void *context)
{
int addrListSize, j;
static const unsigned int maxStackSize = 256;
char **namedSw, **i;
void *stackSize[maxStackSize];
StatError_t statError;
graphlib_error_t graphlibError;
graphlib_graph_p prefixTree2d = NULL, prefixTree3d = NULL;
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "STATD intercepted signal %d\n", sig);
registerSignalHandlers(false);
if (swDebugFile_)
{
addrListSize = backtrace(stackSize, maxStackSize);
namedSw = backtrace_symbols(stackSize, addrListSize);
if (namedSw)
{
fprintf(swDebugFile_, "Stacktrace upon signal %d:\n", sig);
for (i = namedSw, j = 0; j < addrListSize; i++, j++)
{
if (namedSw[j])
fprintf(swDebugFile_, "%p - %s\n", stackSize[j], namedSw[j]);
}
fflush(swDebugFile_);
}
swDebugBufferToFile();
}
if (strcmp(outDir_, "NULL") != 0 && strcmp(filePrefix_, "NULL") != 0)
{
statError = generateGraphs(&prefixTree2d, &prefixTree3d);
if (statError == STAT_OK)
{
extern int gStatGraphRoutinesTotalWidth;
char outFile[BUFSIZE];
gStatGraphRoutinesTotalWidth = statBitVectorLength(proctabSize_);
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Exporting 2D graph to dot\n");
graphlibError = graphlib_colorGraphByLeadingEdgeLabel(prefixTree2d);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "graphlib error coloring graph by leading edge label\n");
if (sig != SIGTERM)
abort();
else
exit(sig);
}
graphlibError = graphlib_scaleNodeWidth(prefixTree2d, 80, 160);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "graphlib error scaling node width\n");
if (sig != SIGTERM)
abort();
else
exit(sig);
}
snprintf(outFile, BUFSIZE, "%s/%s.BE_%s_%d.dot", outDir_, filePrefix_, localHostName_, myRank_);
graphlibError = graphlib_exportGraph(outFile, GRF_DOT, prefixTree2d);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "graphlib error exporting graph to dot format\n");
if (sig != SIGTERM)
abort();
else
exit(sig);
}
}
}
if (sig != SIGTERM)
abort();
else
exit(sig);
}
void STAT_BackEnd::registerSignalHandlers(bool enable)
{
struct sigaction action;
bzero(&action, sizeof(struct sigaction));
if (!enable)
{
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "de-registering signal handlers\n");
action.sa_handler = SIG_DFL;
}
else
{
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "registering signal handlers\n");
action.sa_sigaction = onCrashWrap;
action.sa_flags = SA_SIGINFO;
}
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGBUS, &action, NULL);
sigaction(SIGABRT, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
}
StatError_t STAT_BackEnd::addSerialProcess(const char *pidString)
{
static int rank = -1;
unsigned int pid;
char *remoteNode = NULL, *executablePath = NULL;
string::size_type delimPos;
string remotePid = pidString, remoteHost;
rank++;
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Adding serial process %s with rank %d\n", pidString, rank);
delimPos = remotePid.find_first_of("@");
if (delimPos != string::npos)
{
executablePath = strdup(remotePid.substr(0, delimPos).c_str());
if (executablePath == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s Failed on call to strdup(%s) to executablePath\n", strerror(errno), remotePid.substr(0, delimPos).c_str());
return STAT_ALLOCATE_ERROR;
}
remotePid = remotePid.substr(delimPos + 1, remotePid.length());
}
else
{
executablePath = strdup("output");
if (executablePath == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s Failed on call to strdup to executablePath\n", strerror(errno));
return STAT_ALLOCATE_ERROR;
}
}
delimPos = remotePid.find_first_of(":");
if (delimPos != string::npos)
{
remoteHost = remotePid.substr(0, delimPos);
remoteNode = strdup(remoteHost.c_str());
if (remoteNode == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s Failed to strdup(%s) to remoteNode\n", strerror(errno), remotePid.c_str());
return STAT_ALLOCATE_ERROR;
}
pid = atoi((remotePid.substr(delimPos + 1, remotePid.length())).c_str());
}
else
pid = atoi(remotePid.c_str());
if (strcmp(remoteHost.c_str(), "localhost") == 0 || strcmp(remoteHost.c_str(), localHostName_) == 0 || strcmp(remoteHost.c_str(), localIp_) == 0)
{
proctabSize_++;
proctab_ = (StatBackEndProcInfo_t *)realloc(proctab_, proctabSize_ * sizeof(StatBackEndProcInfo_t));
if (proctab_ == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s: Failed to allocate memory for the process table\n", strerror(errno));
free(remoteNode);
return STAT_ALLOCATE_ERROR;
}
proctab_[proctabSize_ - 1].mpirank = rank;
if (remoteNode != NULL)
proctab_[proctabSize_ - 1].host_name = remoteNode;
else
{
proctab_[proctabSize_ - 1].host_name = strdup("localhost");
if (proctab_[proctabSize_ - 1].host_name == NULL)
{
printMsg(STAT_ALLOCATE_ERROR, __FILE__, __LINE__, "%s Failed on call to strdup to proctab_[%d]\n", strerror(errno), proctabSize_ - 1);
return STAT_ALLOCATE_ERROR;
}
}
proctab_[proctabSize_ - 1].executable_name = executablePath;
proctab_[proctabSize_ - 1].pid = pid;
}
initialized_ = true;
return STAT_OK;
}
#ifdef DYSECTAPI
DysectAPI::BE *STAT_BackEnd::getDysectBe()
{
return dysectBE_;
}
#endif
StatError_t STAT_BackEnd::mainLoop()
{
int tag = 0, ackTag, intRet, nEqClasses, swNotificationFd, mrnNotificationFd, maxFd, nodeId, bitVectorLength, stopArrayLen, majorVersion, minorVersion, revisionVersion;
unsigned int nTraces, traceFrequency, nTasks, functionFanout, maxDepth, nRetries, retryFrequency, *stopArray = NULL, previousSampleType = 0;
uint64_t byteArrayLen = 0;
char *byteArray = NULL, *variableSpecification = NULL;
bool boolRet, recvShouldBlock;
Stream *stream = NULL;
fd_set readFds, writeFds, exceptFds;
PacketPtr packet;
StatError_t statError;
graphlib_error_t graphlibError;
graphlib_graph_p prefixTree2d = NULL, prefixTree3d = NULL;
StatBitVectorEdge_t *edge;
map<unsigned int, Walker *>::iterator processMapIter;
ProcDebug *pDebug = NULL;
#ifdef STAT_FGFS
MRNetSymbolReaderFactory *msrf = NULL;
#endif
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Beginning to execute main loop\n");
if (connected_ == false)
{
printMsg(STAT_NOT_CONNECTED_ERROR, __FILE__, __LINE__, "Not connected to MRNet\n");
return STAT_NOT_CONNECTED_ERROR;
}
do
{
#if defined(GROUP_OPS)
if (doGroupOps_ && usingGdb_ == false)
{
#ifdef DYSECTAPI
/* Let BPatch handle its events */
bpatch_.pollForStatusChange();
#endif
}
#endif
/* Set the stackwalker notification file descriptor */
if (processMap_.size() > 0 && processMapNonNull_ > 0 && usingGdb_ == false)
swNotificationFd = ProcDebug::getNotificationFD();
else
swNotificationFd = -1;
if (swNotificationFd != -1)
{
/* set the MRNet notification FD if StackWalker FD set, otherwise we can just use blocking receives */
mrnNotificationFd = network_->get_EventNotificationFd(MRN::Event::DATA_EVENT);
/* setup the select call for the MRNet and SW FDs */
if (mrnNotificationFd > swNotificationFd)
maxFd = mrnNotificationFd + 1;
else
maxFd = swNotificationFd + 1;
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
FD_ZERO(&exceptFds);
FD_SET(mrnNotificationFd, &readFds);
FD_SET(swNotificationFd, &readFds);
/* call select to get pending requests */
intRet = select(maxFd, &readFds, &writeFds, &exceptFds, NULL);
if (intRet < 0 && errno != EINTR)
{
printMsg(STAT_SYSTEM_ERROR, __FILE__, __LINE__, "%s: select failed\n", strerror(errno));
return STAT_SYSTEM_ERROR;
}
if (FD_ISSET(mrnNotificationFd, &readFds))
{
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Handling MRNet event on FD %d\n", mrnNotificationFd);
network_->clear_EventNotificationFd(MRN::Event::DATA_EVENT);
}
else if (FD_ISSET(swNotificationFd, &readFds))
{
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Handling debug event on FD %d\n", swNotificationFd);
boolRet = ProcDebug::handleDebugEvent(false);
if (boolRet == false)
{
printMsg(STAT_STACKWALKER_ERROR, __FILE__, __LINE__, "Error handling debug events: %s\n", Stackwalker::getLastErrorMsg());
return STAT_STACKWALKER_ERROR;
}
/* Check if target processes exited, if so, set Walker to NULL */
for (processMapIter = processMap_.begin(); processMapIter != processMap_.end(); processMapIter++)
{
if (processMapIter->second == NULL)
continue;
pDebug = dynamic_cast<ProcDebug *>(processMapIter->second->getProcessState());
if (pDebug != NULL)
{
if (pDebug->isTerminated() == true)
{
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Task %d has exited\n", processMapIter->first);
delete processMapIter->second;
processMapNonNull_ = processMapNonNull_ - 1;
processMapIter->second = NULL;
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Process map now contains %d processes, non-null count = %d\n", processMap_.size(), processMapNonNull_);
}
}
}
continue;
}
} /* if (swNotificationFd != -1) */
/* Receive the packet from the STAT FE */
/* TODO blocking here breaks Dysect (hangs) */
//recvShouldBlock = (swNotificationFd == -1);
recvShouldBlock=false;
intRet = network_->recv(&tag, packet, &stream, recvShouldBlock);
if (intRet == 0)
{
#ifdef DYSECTAPI
if (dysectBE_)
{
DysectAPI::DysectErrorCode dysectRet = dysectBE_->handleAll();
if (dysectRet != DysectAPI::OK)
{
printMsg(STAT_STACKWALKER_ERROR, __FILE__, __LINE__, "failure on call to dysectBE_ hanldeAll\n");
return STAT_STACKWALKER_ERROR;
}
}
#endif
continue;
}
else if (intRet != 1)
{
printMsg(STAT_MRNET_ERROR, __FILE__, __LINE__, "stream::recv() failure %d\n", intRet);
return STAT_MRNET_ERROR;
}
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Packet received with tag %d (first = %d)\n", tag, FirstApplicationTag);
/* Initialize return value to error code */
intRet = 1;
ackTag = -1;
switch(tag)
{
case PROT_ATTACH_APPLICATION:
char *in1, *in2;
if (packet->unpack("%s %s", &in1, &in2) != -1)
{
snprintf(outDir_, BUFSIZE, "%s", in1);
snprintf(filePrefix_, BUFSIZE, "%s", in2);
free(in1);
free(in2);
}
printMsg(STAT_LOG_MESSAGE, __FILE__, __LINE__, "Attach request received for %s %s\n", outDir_, filePrefix_);
statError = attach();
if (statError == STAT_OK)
intRet = 0;
ackTag = PROT_ATTACH_APPLICATION_RESP;
break;
case PROT_PAUSE_APPLICATION:
statError = pause();
if (statError == STAT_OK)
intRet = 0;
ackTag = PROT_PAUSE_APPLICATION_RESP;
break;
case PROT_RESUME_APPLICATION:
statError = resume();
if (statError == STAT_OK)
intRet = 0;
ackTag = PROT_RESUME_APPLICATION_RESP;
break;
case PROT_SAMPLE_TRACES:
if (packet->unpack("%ud %ud %ud %ud %ud %ud %s", &nTraces, &traceFrequency, &nRetries, &retryFrequency, &sampleType_, &threadBvLength_, &variableSpecification) == -1)
{
printMsg(STAT_MRNET_ERROR, __FILE__, __LINE__, "unpack(PROT_SAMPLE_TRACES) failed\n");
if (sendAck(stream, PROT_SAMPLE_TRACES_RESP, intRet) != STAT_OK)
{
printMsg(STAT_MRNET_ERROR, __FILE__, __LINE__, "sendAck(PROT_SAMPLE_TRACES_RESP) failed\n");
return STAT_MRNET_ERROR;
}
return STAT_MRNET_ERROR;
}
/* When switching between count + rep and full list, we need to clear previous samples */
if ((previousSampleType ^ sampleType_) & STAT_SAMPLE_COUNT_REP)
sampleType_ |= STAT_SAMPLE_CLEAR_ON_SAMPLE;
previousSampleType = sampleType_;
statError = sampleStackTraces(nTraces, traceFrequency, nRetries, retryFrequency, variableSpecification);
if (statError == STAT_OK)
intRet = 0;
statError = generateGraphs(&prefixTree2d, &prefixTree3d);
if (statError != STAT_OK)
intRet = 1;
ackTag = PROT_SAMPLE_TRACES_RESP;
free(variableSpecification);
variableSpecification = NULL;
break;
case PROT_SEND_LAST_TRACE:
/* Free previously allocated byte array */
if (byteArray != NULL)
{
free(byteArray);
byteArray = NULL;
}
graphlibError = graphlib_serializeBasicGraph(prefixTree2d, &byteArray, &byteArrayLen);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Failed to serialize 2D prefix tree\n");
return STAT_GRAPHLIB_ERROR;
}
ackTag = PROT_SEND_LAST_TRACE_RESP;
#ifdef DYSECTAPI
if (dysectBE_)
dysectBE_->setReturnControlToDysect(true);
#endif
break;
case PROT_SEND_TRACES:
/* Free previously allocated byte array */
if (byteArray != NULL)
{
free(byteArray);
byteArray = NULL;
}
graphlibError = graphlib_serializeBasicGraph(prefixTree3d, &byteArray, &byteArrayLen);
if (GRL_IS_FATALERROR(graphlibError))
{
printMsg(STAT_GRAPHLIB_ERROR, __FILE__, __LINE__, "Failed to serialize 3D prefix tree\n");
return STAT_GRAPHLIB_ERROR;
}
ackTag = PROT_SEND_TRACES_RESP;
#ifdef DYSECTAPI
if (dysectBE_)
dysectBE_->setReturnControlToDysect(true);
#endif
break;
case PROT_SEND_NODE_IN_EDGE:
if (packet->unpack("%d", &nodeId) == -1)
{
printMsg(STAT_MRNET_ERROR, __FILE__, __LINE__, "unpack(PROT_SEND_NODE_IN_EDGE) failed\n");
if (sendAck(stream, PROT_SEND_NODE_IN_EDGE_RESP, intRet) != STAT_OK)
{
printMsg(STAT_MRNET_ERROR, __FILE__, __LINE__, "sendAck(PROT_SEND_NODE_IN_EDGE_RESP) failed\n");