Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/libnczarr/zdebug.c
Line
Count
Source (jump to first uncovered line)
1
/*********************************************************************
2
 *   Copyright 2018, UCAR/Unidata
3
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 *********************************************************************/
5
#include "zincludes.h"
6
7
/* Mnemonic */
8
0
#define RAW 1
9
10
/**************************************************/
11
/* Data Structure  */
12
13
struct ZUTEST* zutest = NULL;
14
15
/**************************************************/
16
17
#ifdef ZCATCH
18
/* Place breakpoint here to catch errors close to where they occur*/
19
int
20
zbreakpoint(int err)
21
{
22
    return ncbreakpoint(err);
23
}
24
25
int
26
zthrow(int err, const char* file, const char* fcn, int line)
27
{
28
    if(err == 0) return err;
29
    ncbacktrace();
30
    return zbreakpoint(err);
31
}
32
33
int
34
zreport(int err, const char* msg, const char* file, const char* fcn, int line)
35
{
36
    if(err == 0) return err;
37
    ZLOG(NCLOGWARN,"!!! zreport: err=%d msg=%s",err,msg);
38
    ncbacktrace();
39
    return zbreakpoint(err);
40
}
41
42
#endif /*ZCATCH*/
43
44
/**************************************************/
45
/* Data Structure printers */
46
47
static NClist* reclaim = NULL;
48
static const int maxreclaim = 16;
49
50
static char*
51
capture(char* s)
52
0
{
53
0
    if(s != NULL) {
54
0
  while(nclistlength(reclaim) >= maxreclaim)
55
0
      free(nclistremove(reclaim,0));
56
0
        if(reclaim == NULL) reclaim = nclistnew();
57
0
        nclistpush(reclaim,s);
58
0
    }
59
0
    return s;
60
0
}
61
62
void
63
nczprint_reclaim(void)
64
0
{
65
0
    if(reclaim != NULL) {
66
0
        nclistfreeall(reclaim);
67
0
  reclaim = NULL;
68
0
    }
69
0
}
70
71
char*
72
nczprint_slice(const NCZSlice slice)
73
0
{
74
0
    return nczprint_slicex(slice,!RAW);
75
0
}
76
77
char*
78
nczprint_slicex(const NCZSlice slice, int raw)
79
0
{
80
0
    char* result = NULL;
81
0
    NCbytes* buf = ncbytesnew();
82
0
    char value[64];
83
84
0
    if(raw)
85
0
        ncbytescat(buf,"[");
86
0
    else
87
0
        ncbytescat(buf,"Slice{");
88
0
    snprintf(value,sizeof(value),"%lu",(unsigned long)slice.start);
89
0
    ncbytescat(buf,value);
90
0
    ncbytescat(buf,":");
91
0
    snprintf(value,sizeof(value),"%lu",(unsigned long)slice.stop);
92
0
    ncbytescat(buf,value);
93
0
    if(slice.stride != 1) {
94
0
        ncbytescat(buf,":");
95
0
        snprintf(value,sizeof(value),"%lu",(unsigned long)slice.stride);
96
0
        ncbytescat(buf,value);
97
0
    }
98
0
    ncbytescat(buf,"|");
99
0
    snprintf(value,sizeof(value),"%lu",(unsigned long)slice.len);
100
0
    ncbytescat(buf,value);
101
0
    if(raw)
102
0
        ncbytescat(buf,"]");
103
0
    else
104
0
        ncbytescat(buf,"}");
105
0
    result = ncbytesextract(buf);
106
0
    ncbytesfree(buf);
107
0
    return capture(result);
108
0
}
109
110
char*
111
nczprint_slices(int rank, const NCZSlice* slices)
112
0
{
113
0
    return nczprint_slicesx(rank, slices, !RAW);
114
0
}
115
116
char*
117
nczprint_slicesx(int rank, const NCZSlice* slices, int raw)
118
0
{
119
0
    int i;
120
0
    char* result = NULL;
121
0
    NCbytes* buf = ncbytesnew();
122
123
0
    for(i=0;i<rank;i++) {
124
0
  char* ssl;
125
0
  if(!raw)
126
0
            ncbytescat(buf,"[");
127
0
  ssl = nczprint_slicex(slices[i],raw);
128
0
  ncbytescat(buf,ssl);
129
0
  if(!raw)
130
0
      ncbytescat(buf,"]");
131
0
    }
132
0
    result = ncbytesextract(buf);
133
0
    ncbytesfree(buf);
134
0
    return capture(result);
135
0
}
136
137
char*
138
nczprint_slab(int rank, const NCZSlice* slices)
139
0
{
140
0
    return nczprint_slicesx(rank,slices,RAW);
141
0
}
142
143
char*
144
nczprint_odom(const NCZOdometer* odom)
145
0
{
146
0
    char* result = NULL;
147
0
    NCbytes* buf = ncbytesnew();
148
0
    char value[128];
149
0
    char* txt = NULL;
150
151
0
    snprintf(value,sizeof(value),"Odometer{rank=%d ",odom->rank);
152
0
    ncbytescat(buf,value);
153
154
0
    ncbytescat(buf," start=");
155
0
    txt = nczprint_vector(odom->rank,odom->start);
156
0
    ncbytescat(buf,txt);
157
0
    ncbytescat(buf," stop=");
158
0
    txt = nczprint_vector(odom->rank,odom->stop);
159
0
    ncbytescat(buf,txt);
160
0
    ncbytescat(buf," len=");
161
0
    txt = nczprint_vector(odom->rank,odom->len);
162
0
    ncbytescat(buf,txt);
163
0
    ncbytescat(buf," stride=");
164
0
    txt = nczprint_vector(odom->rank,odom->stride);
165
0
    ncbytescat(buf,txt);
166
0
    ncbytescat(buf," index=");
167
0
    txt = nczprint_vector(odom->rank,odom->index);
168
0
    ncbytescat(buf,txt);
169
0
    ncbytescat(buf," offset=");
170
0
    snprintf(value,sizeof(value),"%llu",nczodom_offset(odom));
171
0
    ncbytescat(buf,value);
172
0
    ncbytescat(buf," avail=");
173
0
    snprintf(value,sizeof(value),"%llu",nczodom_avail(odom));
174
0
    ncbytescat(buf,value);
175
0
    ncbytescat(buf," more=");
176
0
    snprintf(value,sizeof(value),"%d",nczodom_more(odom));
177
0
    ncbytescat(buf,value);
178
    
179
0
    ncbytescat(buf,"}");
180
0
    result = ncbytesextract(buf);
181
0
    ncbytesfree(buf);
182
0
    return capture(result);
183
0
}
184
185
char*
186
nczprint_projection(const NCZProjection proj)
187
0
{
188
0
   return nczprint_projectionx(proj,!RAW);
189
0
}
190
191
char*
192
nczprint_projectionx(const NCZProjection proj, int raw)
193
0
{
194
0
    char* result = NULL;
195
0
    NCbytes* buf = ncbytesnew();
196
0
    char value[128];
197
198
0
    ncbytescat(buf,"Projection{");
199
0
    snprintf(value,sizeof(value),"id=%d,",proj.id);
200
0
    ncbytescat(buf,value);
201
0
    if(proj.skip) ncbytescat(buf,"*");
202
0
    snprintf(value,sizeof(value),"chunkindex=%lu",(unsigned long)proj.chunkindex);
203
0
    ncbytescat(buf,value);
204
0
    snprintf(value,sizeof(value),",first=%lu",(unsigned long)proj.first);
205
0
    ncbytescat(buf,value);
206
0
    snprintf(value,sizeof(value),",last=%lu",(unsigned long)proj.last);
207
0
    ncbytescat(buf,value);
208
0
    snprintf(value,sizeof(value),",limit=%lu",(unsigned long)proj.limit);
209
0
    ncbytescat(buf,value);
210
0
    snprintf(value,sizeof(value),",iopos=%lu",(unsigned long)proj.iopos);
211
0
    ncbytescat(buf,value);
212
0
    snprintf(value,sizeof(value),",iocount=%lu",(unsigned long)proj.iocount);
213
0
    ncbytescat(buf,value);
214
0
    ncbytescat(buf,",chunkslice=");
215
0
    result = nczprint_slicex(proj.chunkslice,raw);
216
0
    ncbytescat(buf,result);
217
0
    ncbytescat(buf,",memslice=");
218
0
    result = nczprint_slicex(proj.memslice,raw);
219
0
    ncbytescat(buf,result);
220
0
    result = ncbytesextract(buf);
221
0
    ncbytesfree(buf);
222
0
    return capture(result);
223
0
}
224
225
char*
226
nczprint_allsliceprojections(int r, const NCZSliceProjections* slp)
227
0
{
228
0
    int i;
229
0
    char* s;    
230
0
    NCbytes* buf = ncbytesnew();
231
0
    for(i=0;i<r;i++) {
232
0
  s = nczprint_sliceprojections(slp[i]);
233
0
  ncbytescat(buf,s);
234
0
    }  
235
0
    s = ncbytesextract(buf);
236
0
    ncbytesfree(buf);
237
0
    return capture(s);
238
0
}
239
240
char*
241
nczprint_sliceprojections(const NCZSliceProjections slp)
242
0
{
243
0
    return nczprint_sliceprojectionsx(slp,!RAW);
244
0
}
245
246
char*
247
nczprint_sliceprojectionsx(const NCZSliceProjections slp, int raw)
248
0
{
249
0
    char* result = NULL;
250
0
    NCbytes* buf = ncbytesnew();
251
0
    char tmp[4096];
252
0
    int i;
253
254
0
    snprintf(tmp,sizeof(tmp),"SliceProjection{r=%d range=%s count=%ld",
255
0
        slp.r,nczprint_chunkrange(slp.range),(long)slp.count);
256
0
    ncbytescat(buf,tmp);
257
0
    ncbytescat(buf,",projections=[\n");
258
0
    for(i=0;i<slp.count;i++) {
259
0
  NCZProjection* p = (NCZProjection*)&slp.projections[i];
260
0
  ncbytescat(buf,"\t");
261
0
        result = nczprint_projectionx(*p,raw);
262
0
        ncbytescat(buf,result);
263
0
  ncbytescat(buf,"\n");
264
0
    }
265
0
    result = NULL;
266
0
    ncbytescat(buf,"]");
267
0
    ncbytescat(buf,"}\n");
268
0
    result = ncbytesextract(buf);
269
0
    ncbytesfree(buf);
270
0
    return capture(result);
271
0
}
272
273
char*
274
nczprint_chunkrange(const NCZChunkRange range)
275
0
{
276
0
    char* result = NULL;
277
0
    NCbytes* buf = ncbytesnew();
278
0
    char digits[64];
279
280
0
    ncbytescat(buf,"ChunkRange{start=");
281
0
    snprintf(digits,sizeof(digits),"%llu",range.start);
282
0
    ncbytescat(buf,digits);
283
0
    ncbytescat(buf," stop=");
284
0
    snprintf(digits,sizeof(digits),"%llu",range.stop);
285
0
    ncbytescat(buf,digits);
286
0
    ncbytescat(buf,"}");
287
0
    result = ncbytesextract(buf);
288
0
    ncbytesfree(buf);
289
0
    return capture(result);
290
0
}
291
292
char*
293
nczprint_idvector(size_t len, const int* ids)
294
0
{
295
0
    size64_t v[4096];
296
0
    size_t i;
297
0
    for(i=0;i<len;i++) v[i] = ids[i];    
298
0
    return nczprint_vector(len,v);
299
0
}
300
301
char*
302
nczprint_paramvector(size_t len, const unsigned* params)
303
0
{
304
0
    size64_t v[4096];
305
0
    size_t i;
306
0
    for(i=0;i<len;i++) v[i] = params[i];    
307
0
    return nczprint_vector(len,v);
308
0
}
309
310
char*
311
nczprint_sizevector(size_t len, const size_t* sizes)
312
0
{
313
0
    size64_t v[4096];
314
0
    size_t i;
315
0
    for(i=0;i<len;i++) v[i] = sizes[i];    
316
0
    return nczprint_vector(len,v);
317
0
}
318
319
char*
320
nczprint_vector(size_t len, const size64_t* vec)
321
0
{
322
0
    char* result = NULL;
323
0
    int i;
324
0
    char value[128];
325
0
    NCbytes* buf = ncbytesnew();
326
327
0
    ncbytescat(buf,"(");
328
0
    for(i=0;i<len;i++) {
329
0
        if(i > 0) ncbytescat(buf,",");
330
0
        snprintf(value,sizeof(value),"%lu",(unsigned long)vec[i]);  
331
0
  ncbytescat(buf,value);
332
0
    }
333
0
    ncbytescat(buf,")");
334
0
    result = ncbytesextract(buf);
335
0
    ncbytesfree(buf);
336
0
    return capture(result);
337
0
}
338
339
char*
340
nczprint_envv(const char** envv)
341
0
{
342
0
    char* result = NULL;
343
0
    int i;
344
0
    NCbytes* buf = ncbytesnew();
345
0
    const char** p;
346
347
0
    ncbytescat(buf,"(");
348
0
    if(envv) {
349
0
        for(i=0,p=envv;*p;p++,i++) {
350
0
        if(i > 0) ncbytescat(buf,",");
351
0
      ncbytescat(buf,"'");
352
0
      ncbytescat(buf,*p);
353
0
      ncbytescat(buf,"'");
354
0
  }
355
0
    }
356
0
    ncbytescat(buf,")");
357
0
    result = ncbytesextract(buf);
358
0
    ncbytesfree(buf);
359
0
    return capture(result);
360
0
}
361
362
void
363
zdumpcommon(const struct Common* c)
364
0
{
365
0
    int r;
366
0
    fprintf(stderr,"Common:\n");
367
#if 0
368
    fprintf(stderr,"\tfile: %s\n",c->file->controller->path);
369
    fprintf(stderr,"\tvar: %s\n",c->var->hdr.name);
370
    fprintf(stderr,"\treading=%d\n",c->reading);
371
#endif
372
0
    fprintf(stderr,"\trank=%d",c->rank);
373
0
    fprintf(stderr," dimlens=%s",nczprint_vector(c->rank,c->dimlens));
374
0
    fprintf(stderr," chunklens=%s",nczprint_vector(c->rank,c->chunklens));
375
#if 0
376
    fprintf(stderr,"\tmemory=%p\n",c->memory);
377
    fprintf(stderr,"\ttypesize=%d\n",c->typesize);
378
    fprintf(stderr,"\tswap=%d\n",c->swap);
379
#endif
380
0
    fprintf(stderr," shape=%s\n",nczprint_vector(c->rank,c->shape));
381
0
    fprintf(stderr,"\tallprojections:\n");
382
0
    for(r=0;r<c->rank;r++)
383
0
        fprintf(stderr,"\t\t[%d] %s\n",r,nczprint_sliceprojectionsx(c->allprojections[r],RAW));
384
0
    fflush(stderr);
385
0
}