@@ -154,6 +154,26 @@ print_totals (uint64_t usecs)
154
154
free (rate);
155
155
}
156
156
157
+ static void
158
+ inc_blksize_ctr (blksize_hist_t &hist, size_t blksize)
159
+ {
160
+ static bool out_of_memory = false ;
161
+ if (out_of_memory)
162
+ return ;
163
+
164
+ ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock);
165
+
166
+ try {
167
+ hist[blksize]++;
168
+ }
169
+ catch (std::bad_alloc) {
170
+ // Avoid reporting the same error over and over again
171
+ nbdkit_error (" out of memory for blocksize statistics" );
172
+ out_of_memory = true ;
173
+ return ;
174
+ }
175
+ }
176
+
157
177
static void
158
178
print_histogram (const blksize_hist_t hist, int count)
159
179
{
@@ -324,11 +344,7 @@ stats_pread (nbdkit_next *next,
324
344
struct timeval start;
325
345
int r;
326
346
327
- {
328
- ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock);
329
- blksize_pread_st[count]++;
330
- }
331
-
347
+ inc_blksize_ctr (blksize_pread_st, count);
332
348
gettimeofday (&start, NULL );
333
349
r = next->pread (next, buf, count, offset, flags, err);
334
350
if (r == 0 ) record_stat (&pread_st, count, &start);
@@ -345,11 +361,7 @@ stats_pwrite (nbdkit_next *next,
345
361
struct timeval start;
346
362
int r;
347
363
348
- {
349
- ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock);
350
- blksize_pwrite_st[count]++;
351
- }
352
-
364
+ inc_blksize_ctr (blksize_pwrite_st, count);
353
365
gettimeofday (&start, NULL );
354
366
r = next->pwrite (next, buf, count, offset, flags, err);
355
367
if (r == 0 ) record_stat (&pwrite_st, count, &start);
@@ -366,11 +378,7 @@ stats_trim (nbdkit_next *next,
366
378
struct timeval start;
367
379
int r;
368
380
369
- {
370
- ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock);
371
- blksize_trim_st[count]++;
372
- }
373
-
381
+ inc_blksize_ctr (blksize_trim_st, count);
374
382
gettimeofday (&start, NULL );
375
383
r = next->trim (next, count, offset, flags, err);
376
384
if (r == 0 ) record_stat (&trim_st, count, &start);
@@ -402,11 +410,7 @@ stats_zero (nbdkit_next *next,
402
410
struct timeval start;
403
411
int r;
404
412
405
- {
406
- ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock);
407
- blksize_zero_st[count]++;
408
- }
409
-
413
+ inc_blksize_ctr (blksize_zero_st, count);
410
414
gettimeofday (&start, NULL );
411
415
r = next->zero (next, count, offset, flags, err);
412
416
if (r == 0 ) record_stat (&zero_st, count, &start);
0 commit comments