@@ -59,6 +59,7 @@ static char *filename;
59
59
static bool append;
60
60
static FILE *fp;
61
61
static struct timeval start_t ;
62
+ static double print_threshold = 0.95 ;
62
63
63
64
typedef struct {
64
65
const char *name;
@@ -158,7 +159,7 @@ static void
158
159
inc_blksize_ctr (blksize_hist_t &hist, size_t blksize)
159
160
{
160
161
static bool out_of_memory = false ;
161
- if (out_of_memory)
162
+ if (out_of_memory || print_threshold == 0 )
162
163
return ;
163
164
164
165
ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock);
@@ -175,7 +176,7 @@ inc_blksize_ctr (blksize_hist_t &hist, size_t blksize)
175
176
}
176
177
177
178
static void
178
- print_histogram (const blksize_hist_t & hist, int count )
179
+ print_histogram (const blksize_hist_t & hist)
179
180
{
180
181
double total = 0 ;
181
182
for (auto el : hist) {
@@ -184,38 +185,48 @@ print_histogram (const blksize_hist_t& hist, int count)
184
185
185
186
// Sort
186
187
auto pairs = std::vector<std::pair<size_t , size_t >> (hist.begin (), hist.end ());
187
- std::sort (pairs.begin (), pairs.end (),
188
- [](decltype (pairs[0 ]) a, decltype (pairs[0 ]) b) {
189
- return a.second > b.second ;
190
- });
191
-
192
- int i = 0 ;
188
+ std::sort (pairs.begin (), pairs.end (),
189
+ [](decltype (pairs[0 ]) a, decltype (pairs[0 ]) b) {
190
+ return a.second > b.second ;
191
+ });
192
+
193
+ // Print values until we have covered *print_threshold* percent of
194
+ // requests.
195
+ size_t to_print = static_cast <ssize_t >(print_threshold * total);
196
+ size_t printed = 0 ;
193
197
for (auto el : pairs) {
194
- if (++i >= count)
198
+ if (printed >= to_print) {
199
+ size_t requests = total - printed;
200
+ fprintf (fp, " (others) %9zu (%.2f%%)\n " ,
201
+ requests, static_cast <double >(requests) / total * 100 );
195
202
break ;
203
+ }
204
+ size_t blocksize = el.first ;
205
+ size_t requests = el.second ;
196
206
fprintf (fp, " %13zu %9zu (%.2f%%)\n " ,
197
- el.first , el.second , static_cast <double >(el.second ) / total * 100 );
207
+ blocksize, requests, static_cast <double >(requests) / total * 100 );
208
+ printed += requests;
198
209
}
199
210
}
200
211
201
212
static void
202
213
print_blocksize_stats (void )
203
214
{
204
- fprintf (fp, " \n READ Request sizes (top 28) :\n " );
215
+ fprintf (fp, " \n READ Request sizes:\n " );
205
216
fprintf (fp, " blocksize request count\n " );
206
- print_histogram (blksize_pread_st, 28 );
207
-
208
- fprintf (fp, " \n WRITE Request sizes (top 28) :\n " );
217
+ print_histogram (blksize_pread_st);
218
+
219
+ fprintf (fp, " \n WRITE Request sizes:\n " );
209
220
fprintf (fp, " blocksize request count\n " );
210
- print_histogram (blksize_pwrite_st, 28 );
221
+ print_histogram (blksize_pwrite_st);
211
222
212
- fprintf (fp, " \n TRIM Request sizes (top 28) :\n " );
223
+ fprintf (fp, " \n TRIM Request sizes:\n " );
213
224
fprintf (fp, " blocksize request count\n " );
214
- print_histogram (blksize_trim_st, 28 );
215
-
216
- fprintf (fp, " \n ZERO Request sizes (top 28) :\n " );
225
+ print_histogram (blksize_trim_st);
226
+
227
+ fprintf (fp, " \n ZERO Request sizes:\n " );
217
228
fprintf (fp, " blocksize request count\n " );
218
- print_histogram (blksize_zero_st, 28 );
229
+ print_histogram (blksize_zero_st);
219
230
}
220
231
221
232
static inline void
@@ -229,7 +240,8 @@ print_stats (int64_t usecs)
229
240
print_stat (&extents_st, usecs);
230
241
print_stat (&cache_st, usecs);
231
242
print_stat (&flush_st, usecs);
232
- print_blocksize_stats ();
243
+ if (print_threshold != 0 )
244
+ print_blocksize_stats ();
233
245
fflush (fp);
234
246
}
235
247
@@ -271,6 +283,18 @@ stats_config (nbdkit_next_config *next, nbdkit_backend *nxdata,
271
283
append = r;
272
284
return 0 ;
273
285
}
286
+ else if (strcmp (key, " statsthreshold" ) == 0 ) {
287
+ int ival;
288
+ r = nbdkit_parse_int (" printing threshold" , value, &ival);
289
+ if (r == -1 )
290
+ return -1 ;
291
+ if (ival > 100 or ival < 0 ) {
292
+ nbdkit_error (" statsthreshold must be between 0 and 100 (percent)" );
293
+ return -1 ;
294
+ }
295
+ print_threshold = static_cast <double >(ival) / 100 ;
296
+ return 0 ;
297
+ }
274
298
275
299
return next (nxdata, key, value);
276
300
}
0 commit comments