xref: /petsc/src/sys/error/pstack.c (revision d45a07a79bb1e93ae6fa85d4a4175cd20a6ac3a0)
1 
2 #include <petscsys.h>        /*I  "petscsys.h"   I*/
3 
4 #if defined(PETSC_USE_DEBUG)
5 
6 #if defined(PETSC_HAVE_PTHREADCLASSES)
7 #if defined(PETSC_PTHREAD_LOCAL)
8 PETSC_PTHREAD_LOCAL PetscStack *petscstack = 0;
9 #endif
10 #else
11 PetscStack *petscstack = 0;
12 #endif
13 
14 
15 #if defined(PETSC_HAVE_SAWS)
16 #include <petscviewersaws.h>
17 
18 static PetscBool amsmemstack = PETSC_FALSE;
19 
20 #undef __FUNCT__
21 #define __FUNCT__ "PetscStackSAWsGrantAccess"
22 /*@C
23    PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher
24 
25    Collective on PETSC_COMM_WORLD?
26 
27    Level: developer
28 
29    Concepts: publishing object
30 
31    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
32 
33 .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
34 
35 @*/
36 void  PetscStackSAWsGrantAccess(void)
37 {
38   if (amsmemstack) {
39     /* ignore any errors from SAWs */
40     SAWs_Unlock();
41   }
42 }
43 
44 #undef __FUNCT__
45 #define __FUNCT__ "PetscStackSAWsTakeAccess"
46 /*@C
47    PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames to the SAWs publisher
48 
49    Collective on PETSC_COMM_WORLD?
50 
51    Level: developer
52 
53    Concepts: publishing object
54 
55    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
56 
57 .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
58 
59 @*/
60 void  PetscStackSAWsTakeAccess(void)
61 {
62   if (amsmemstack) {
63     /* ignore any errors from SAWs */
64     SAWs_Lock();
65   }
66 }
67 
68 PetscErrorCode PetscStackViewSAWs(void)
69 {
70   PetscStack*    petscstackp;
71   PetscMPIInt    rank;
72   PetscErrorCode ierr;
73 
74   ierr  = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
75   if (rank) return 0;
76   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
77   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstackp->function,20,SAWs_READ,SAWs_STRING));
78   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/_current_size",&petscstackp->currentsize,1,SAWs_READ,SAWs_INT));
79   amsmemstack = PETSC_TRUE;
80   return 0;
81 }
82 
83 #undef __FUNCT__
84 #define __FUNCT__ "PetscStackSAWsViewOff"
85 PetscErrorCode PetscStackSAWsViewOff(void)
86 {
87   PetscFunctionBegin;
88   if (!amsmemstack) PetscFunctionReturn(0);
89   PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack"));
90   amsmemstack = PETSC_FALSE;
91   PetscFunctionReturn(0);
92 }
93 
94 #endif
95 
96 PetscErrorCode PetscStackCreate(void)
97 {
98   PetscStack *petscstack_in;
99   if (PetscStackActive()) return 0;
100 
101   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
102   petscstack_in->currentsize = 0;
103   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
104 
105 #if defined(PETSC_HAVE_SAWS)
106   {
107   PetscBool flg = PETSC_FALSE;
108   PetscOptionsHasName(NULL,"-stack_view",&flg);
109   if (flg) PetscStackViewSAWs();
110   }
111 #endif
112   return 0;
113 }
114 
115 
116 #undef __FUNCT__
117 #define __FUNCT__ "PetscStackView"
118 PetscErrorCode  PetscStackView(FILE *file)
119 {
120   int        i;
121   PetscStack *petscstackp;
122 
123   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
124   if (!file) file = PETSC_STDOUT;
125 
126   if (file == PETSC_STDOUT) {
127     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
128     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
129     (*PetscErrorPrintf)("      is given.\n");
130     for (i=petscstackp->currentsize-1; i>=0; i--) (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
131   } else {
132     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
133     fprintf(file,"      INSTEAD the line number of the start of the function\n");
134     fprintf(file,"      is given.\n");
135     for (i=petscstackp->currentsize-1; i>=0; i--) fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
136   }
137   return 0;
138 }
139 
140 PetscErrorCode PetscStackDestroy(void)
141 {
142   if (PetscStackActive()) {
143     PetscStack *petscstack_in;
144     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
145     free(petscstack_in);
146     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
147     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
148   }
149   return 0;
150 }
151 
152 #undef __FUNCT__
153 #define __FUNCT__ "PetscStackCopy"
154 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
155 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
156 {
157   int i;
158 
159   if (!sint) sout->currentsize = 0;
160   else {
161     for (i=0; i<sint->currentsize; i++) {
162       sout->function[i]     = sint->function[i];
163       sout->file[i]         = sint->file[i];
164       sout->directory[i]    = sint->directory[i];
165       sout->line[i]         = sint->line[i];
166       sout->petscroutine[i] = sint->petscroutine[i];
167     }
168     sout->currentsize = sint->currentsize;
169   }
170   return 0;
171 }
172 
173 #undef __FUNCT__
174 #define __FUNCT__ "PetscStackPrint"
175 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
176 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
177 {
178   int i;
179 
180   if (!sint) return(0);
181   for (i=sint->currentsize-2; i>=0; i--) fprintf(fp,"      [%d]  %s() line %d in %s%s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->directory[i],sint->file[i]);
182   return 0;
183 }
184 
185 #else
186 
187 #undef __FUNCT__
188 #define __FUNCT__ "PetscStackCreate"
189 PetscErrorCode  PetscStackCreate(void)
190 {
191   PetscFunctionBegin;
192   PetscFunctionReturn(0);
193 }
194 #undef __FUNCT__
195 #define __FUNCT__ "PetscStackView"
196 PetscErrorCode  PetscStackView(FILE *file)
197 {
198   PetscFunctionBegin;
199   PetscFunctionReturn(0);
200 }
201 #undef __FUNCT__
202 #define __FUNCT__ "PetscStackDestroy"
203 PetscErrorCode  PetscStackDestroy(void)
204 {
205   PetscFunctionBegin;
206   PetscFunctionReturn(0);
207 }
208 
209 #if defined(PETSC_HAVE_SAWS)     /* SAWs stack functions do nothing in optimized mode */
210 void PetscStackSAWsGrantAccess(void) {}
211 void PetscStackSAWsTakeAccess(void) {}
212 
213 PetscErrorCode PetscStackViewSAWs(void)
214 {
215   return 0;
216 }
217 
218 #undef __FUNCT__
219 #define __FUNCT__ "PetscStackSAWsViewOff"
220 PetscErrorCode  PetscStackSAWsViewOff(void)
221 {
222   PetscFunctionBegin;
223   PetscFunctionReturn(0);
224 }
225 #endif
226 
227 #endif
228 
229