• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cstdio"
17 #include "cstdlib"
18 #include "cstdarg"
19 #include "sys/types.h"
20 #include "sys/stat.h"
21 #include "sys/file.h"
22 #include "fcntl.h"
23 #include "unistd.h"
24 #include "util.h"
25 
26 using namespace std;
27 
28 constexpr int SSCANF_SIZE = 32;
29 constexpr int BUFFERSIZE = 100;
30 char g_buffer[BUFFERSIZE];
31 
MyPrintfVf(FILE * stream,const char * format,...)32 int MyPrintfVf(FILE *stream, const char *format, ...)
33 {
34     va_list args;
35     int ret;
36     va_start(args, format);
37     ret = vfprintf(stream, format, args);
38     va_end(args);
39     return (ret);
40 }
41 
MyPrintfVs(const char * format,...)42 int MyPrintfVs(const char *format, ...)
43 {
44     va_list args;
45     int ret;
46     va_start(args, format);
47     ret = vsprintf(g_buffer, format, args);
48     va_end(args);
49     return (ret);
50 }
51 
MyPrintfVsn(const char * format,...)52 int MyPrintfVsn(const char *format, ...)
53 {
54     va_list args;
55     int ret;
56     va_start(args, format);
57     ret = vsnprintf(g_buffer, BUFFERSIZE, format, args);
58     va_end(args);
59     return (ret);
60 }
61 
Bm_function_Fopen_read(benchmark::State & state)62 static void Bm_function_Fopen_read(benchmark::State &state)
63 {
64     for (auto _ : state) {
65         FILE *fp = fopen("/dev/zero", "r");
66         if (fp == nullptr) {
67             perror("fopen read");
68         }
69         benchmark::DoNotOptimize(fp);
70         state.PauseTiming();
71         if (fp != nullptr) {
72             fclose(fp);
73         }
74         state.ResumeTiming();
75     }
76 }
77 
Bm_function_Fopen_write(benchmark::State & state)78 static void Bm_function_Fopen_write(benchmark::State &state)
79 {
80     for (auto _ : state) {
81         FILE *fp = fopen("/dev/zero", "w");
82         if (fp == nullptr) {
83             perror("fopen write");
84         }
85         benchmark::DoNotOptimize(fp);
86         state.PauseTiming();
87         if (fp != nullptr) {
88             fclose(fp);
89         }
90         state.ResumeTiming();
91     }
92 }
93 
Bm_function_Fopen_append(benchmark::State & state)94 static void Bm_function_Fopen_append(benchmark::State &state)
95 {
96     for (auto _ : state) {
97         FILE *fp = fopen("/dev/zero", "a");
98         if (fp == nullptr) {
99             perror("fopen append");
100         }
101         benchmark::DoNotOptimize(fp);
102         state.PauseTiming();
103         if (fp != nullptr) {
104             fclose(fp);
105         }
106         state.ResumeTiming();
107     }
108 }
109 
Bm_function_Fopen_rplus(benchmark::State & state)110 static void Bm_function_Fopen_rplus(benchmark::State &state)
111 {
112     for (auto _ : state) {
113         FILE *fp = fopen("/dev/zero", "r+");
114         if (fp == nullptr) {
115             perror("fopen r+");
116         }
117         benchmark::DoNotOptimize(fp);
118         state.PauseTiming();
119         if (fp != nullptr) {
120             fclose(fp);
121         }
122         state.ResumeTiming();
123     }
124 }
125 
Bm_function_Fopen_wplus(benchmark::State & state)126 static void Bm_function_Fopen_wplus(benchmark::State &state)
127 {
128     for (auto _ : state) {
129         FILE *fp = fopen("/dev/zero", "w+");
130         if (fp == nullptr) {
131             perror("fopen w+");
132         }
133         benchmark::DoNotOptimize(fp);
134         state.PauseTiming();
135         if (fp != nullptr) {
136             fclose(fp);
137         }
138         state.ResumeTiming();
139     }
140 }
141 
Bm_function_Fopen_append_plus(benchmark::State & state)142 static void Bm_function_Fopen_append_plus(benchmark::State &state)
143 {
144     for (auto _ : state) {
145         FILE *fp = fopen("/dev/zero", "a+");
146         if (fp == nullptr) {
147             perror("fopen a+");
148         }
149         benchmark::DoNotOptimize(fp);
150         state.PauseTiming();
151         if (fp != nullptr) {
152             fclose(fp);
153         }
154         state.ResumeTiming();
155     }
156 }
157 
Bm_function_Fopen_rb(benchmark::State & state)158 static void Bm_function_Fopen_rb(benchmark::State &state)
159 {
160     for (auto _ : state) {
161         FILE *fp = fopen("/dev/zero", "rb");
162         if (fp == nullptr) {
163             perror("fopen rb");
164         }
165         benchmark::DoNotOptimize(fp);
166         state.PauseTiming();
167         if (fp != nullptr) {
168             fclose(fp);
169         }
170         state.ResumeTiming();
171     }
172 }
173 
Bm_function_Fopen_wb(benchmark::State & state)174 static void Bm_function_Fopen_wb(benchmark::State &state)
175 {
176     for (auto _ : state) {
177         FILE *fp = fopen("/dev/zero", "wb");
178         if (fp == nullptr) {
179             perror("fopen wb");
180         }
181         benchmark::DoNotOptimize(fp);
182         state.PauseTiming();
183         if (fp != nullptr) {
184             fclose(fp);
185         }
186         state.ResumeTiming();
187     }
188 }
189 
Bm_function_Fopen_ab(benchmark::State & state)190 static void Bm_function_Fopen_ab(benchmark::State &state)
191 {
192     for (auto _ : state) {
193         FILE *fp = fopen("/dev/zero", "ab");
194         if (fp == nullptr) {
195             perror("fopen ab");
196         }
197         benchmark::DoNotOptimize(fp);
198         state.PauseTiming();
199         if (fp != nullptr) {
200             fclose(fp);
201         }
202         state.ResumeTiming();
203     }
204 }
205 
Bm_function_Fopen_rb_plus(benchmark::State & state)206 static void Bm_function_Fopen_rb_plus(benchmark::State &state)
207 {
208     for (auto _ : state) {
209         FILE *fp = fopen("/dev/zero", "rb+");
210         if (fp == nullptr) {
211             perror("fopen rb+");
212         }
213         benchmark::DoNotOptimize(fp);
214         state.PauseTiming();
215         if (fp != nullptr) {
216             fclose(fp);
217         }
218         state.ResumeTiming();
219     }
220 }
221 
Bm_function_Fopen_wb_plus(benchmark::State & state)222 static void Bm_function_Fopen_wb_plus(benchmark::State &state)
223 {
224     for (auto _ : state) {
225         FILE *fp = fopen("/dev/zero", "wb+");
226         if (fp == nullptr) {
227             perror("fopen wb+");
228         }
229         benchmark::DoNotOptimize(fp);
230         state.PauseTiming();
231         if (fp != nullptr) {
232             fclose(fp);
233         }
234         state.ResumeTiming();
235     }
236 }
237 
Bm_function_Fopen_ab_plus(benchmark::State & state)238 static void Bm_function_Fopen_ab_plus(benchmark::State &state)
239 {
240     for (auto _ : state) {
241         FILE *fp = fopen("/dev/zero", "ab+");
242         if (fp == nullptr) {
243             perror("fopen ab+");
244         }
245         benchmark::DoNotOptimize(fp);
246         state.PauseTiming();
247         if (fp != nullptr) {
248             fclose(fp);
249         }
250         state.ResumeTiming();
251     }
252 }
253 
Bm_function_Fclose(benchmark::State & state)254 static void Bm_function_Fclose(benchmark::State &state)
255 {
256     for (auto _ : state) {
257         state.PauseTiming();
258         FILE *fp = fopen("/dev/zero", "w+");
259         if (fp == nullptr) {
260             perror("fclose proc");
261         }
262         if (fp != nullptr) {
263             state.ResumeTiming();
264             benchmark::DoNotOptimize(fclose(fp));
265         }
266     }
267 }
268 
269 // Used to convert a file descriptor to a file pointer
Bm_function_Fdopen(benchmark::State & state)270 static void Bm_function_Fdopen(benchmark::State &state)
271 {
272     for (auto _ : state) {
273         int fp = open("/dev/zero", O_RDONLY, OPEN_MODE);
274         FILE *fd = fdopen(fp, "r");
275         if (fd == nullptr) {
276             perror("fdopen");
277         }
278         benchmark::DoNotOptimize(fd);
279         fclose(fd);
280     }
281 
282     state.SetBytesProcessed(state.iterations());
283 }
284 
285 // Use the parameter list to send formatted output to the stream.
286 // string type
Bm_function_Vfprintf_str(benchmark::State & state)287 static void Bm_function_Vfprintf_str(benchmark::State &state)
288 {
289     FILE *fp = fopen("/dev/zero", "w+");
290     const char *arr1 = "hello";
291     const char *arr2 = "world";
292     for (auto _ : state) {
293         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %s %s success", arr1, arr2));
294     }
295     fclose(fp);
296     state.SetBytesProcessed(state.iterations());
297 }
298 
299 // int type
Bm_function_Vfprintf_int(benchmark::State & state)300 static void Bm_function_Vfprintf_int(benchmark::State &state)
301 {
302     FILE *fp = fopen("/dev/zero", "w+");
303     int arr1 = 233;
304     int arr2 = 322;
305     for (auto _ : state) {
306         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %d %d success", arr1, arr2));
307     }
308     fclose(fp);
309     state.SetBytesProcessed(state.iterations());
310 }
311 
312 // float type
Bm_function_Vfprintf_float(benchmark::State & state)313 static void Bm_function_Vfprintf_float(benchmark::State &state)
314 {
315     FILE *fp = fopen("/dev/zero", "w+");
316     float i = 22.33f;
317     float j = 33.22f;
318     for (auto _ : state) {
319         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %f %f success", i, j));
320     }
321     fclose(fp);
322     state.SetBytesProcessed(state.iterations());
323 }
324 
325 // longdouble type
Bm_function_Vfprintf_longdouble(benchmark::State & state)326 static void Bm_function_Vfprintf_longdouble(benchmark::State &state)
327 {
328     FILE *fp = fopen("/dev/zero", "w+");
329     long double i = 2250996946.3365252546L;
330     long double j = 9583454321234.226342465121L;
331     for (auto _ : state) {
332         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %Lf %Lf success", i, j));
333     }
334     fclose(fp);
335     state.SetBytesProcessed(state.iterations());
336 }
337 
338 // unsigned type
Bm_function_Vfprintf_unsigned(benchmark::State & state)339 static void Bm_function_Vfprintf_unsigned(benchmark::State &state)
340 {
341     FILE *fp = fopen("/dev/zero", "w+");
342     unsigned int i = 4294967295U;
343     unsigned int j = 3456264567U;
344     for (auto _ : state) {
345         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %u %u success", i, j));
346     }
347     fclose(fp);
348     state.SetBytesProcessed(state.iterations());
349 }
350 
351 // long type
Bm_function_Vfprintf_long(benchmark::State & state)352 static void Bm_function_Vfprintf_long(benchmark::State &state)
353 {
354     FILE *fp = fopen("/dev/zero", "w+");
355     long i = 1234567890L;
356     long j = 954611731L;
357     for (auto _ : state) {
358         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %ld %ld success", i, j));
359     }
360     fclose(fp);
361     state.SetBytesProcessed(state.iterations());
362 }
363 
364 // short type
Bm_function_Vfprintf_short(benchmark::State & state)365 static void Bm_function_Vfprintf_short(benchmark::State &state)
366 {
367     FILE *fp = fopen("/dev/zero", "w+");
368     short i = 32767;
369     short j = -32768;
370     for (auto _ : state) {
371         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %hd %hd success", i, j));
372     }
373     fclose(fp);
374     state.SetBytesProcessed(state.iterations());
375 }
376 
377 // char type
Bm_function_Vfprintf_char(benchmark::State & state)378 static void Bm_function_Vfprintf_char(benchmark::State &state)
379 {
380     FILE *fp = fopen("/dev/zero", "w+");
381     char i = 'n';
382     char j = 'Z';
383     for (auto _ : state) {
384         benchmark::DoNotOptimize(MyPrintfVf(fp, "Set parameter %c %c success", i, j));
385     }
386     fclose(fp);
387     state.SetBytesProcessed(state.iterations());
388 }
389 
390 // Use the parameter list to send formatted output to the stream.
391 // string type
Bm_function_Fprintf_str(benchmark::State & state)392 static void Bm_function_Fprintf_str(benchmark::State &state)
393 {
394     FILE *fp = fopen("/dev/zero", "w+");
395     const char *arr1 = "hello";
396     const char *arr2 = "world";
397     for (auto _ : state) {
398         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %s %s success", arr1, arr2));
399     }
400     fclose(fp);
401     state.SetBytesProcessed(state.iterations());
402 }
403 
404 // int type
Bm_function_Fprintf_int(benchmark::State & state)405 static void Bm_function_Fprintf_int(benchmark::State &state)
406 {
407     FILE *fp = fopen("/dev/zero", "w+");
408     int arr1 = 233;
409     int arr2 = 322;
410     for (auto _ : state) {
411         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %d %d success", arr1, arr2));
412     }
413     fclose(fp);
414     state.SetBytesProcessed(state.iterations());
415 }
416 
417 // float type
Bm_function_Fprintf_float(benchmark::State & state)418 static void Bm_function_Fprintf_float(benchmark::State &state)
419 {
420     FILE *fp = fopen("/dev/zero", "w+");
421     float i = 22.33f;
422     float j = 33.22f;
423     for (auto _ : state) {
424         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %f %f success", i, j));
425     }
426     fclose(fp);
427     state.SetBytesProcessed(state.iterations());
428 }
429 
430 // longdouble type
Bm_function_Fprintf_longdouble(benchmark::State & state)431 static void Bm_function_Fprintf_longdouble(benchmark::State &state)
432 {
433     FILE *fp = fopen("/dev/zero", "w+");
434     long double i = 2250996946.3365252546L;
435     long double j = 9583454321234.226342465121L;
436     for (auto _ : state) {
437         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %Lf %Lf success", i, j));
438     }
439     fclose(fp);
440     state.SetBytesProcessed(state.iterations());
441 }
442 
443 // unsigned type
Bm_function_Fprintf_unsigned(benchmark::State & state)444 static void Bm_function_Fprintf_unsigned(benchmark::State &state)
445 {
446     FILE *fp = fopen("/dev/zero", "w+");
447     unsigned int i = 4294967295U;
448     unsigned int j = 3456264567U;
449     for (auto _ : state) {
450         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %u %u success", i, j));
451     }
452     fclose(fp);
453     state.SetBytesProcessed(state.iterations());
454 }
455 
456 // long type
Bm_function_Fprintf_long(benchmark::State & state)457 static void Bm_function_Fprintf_long(benchmark::State &state)
458 {
459     FILE *fp = fopen("/dev/zero", "w+");
460     long i = 1234567890L;
461     long j = 954611731L;
462     for (auto _ : state) {
463         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %ld %ld success", i, j));
464     }
465     fclose(fp);
466     state.SetBytesProcessed(state.iterations());
467 }
468 
469 // short type
Bm_function_Fprintf_short(benchmark::State & state)470 static void Bm_function_Fprintf_short(benchmark::State &state)
471 {
472     FILE *fp = fopen("/dev/zero", "w+");
473     short i = 32767;
474     short j = -32768;
475     for (auto _ : state) {
476         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %hd %hd success", i, j));
477     }
478     fclose(fp);
479     state.SetBytesProcessed(state.iterations());
480 }
481 
482 // char type
Bm_function_Fprintf_char(benchmark::State & state)483 static void Bm_function_Fprintf_char(benchmark::State &state)
484 {
485     FILE *fp = fopen("/dev/zero", "w+");
486     char i = 'n';
487     char j = 'Z';
488     for (auto _ : state) {
489         benchmark::DoNotOptimize(fprintf(fp, "Set parameter %c %c success", i, j));
490     }
491     fclose(fp);
492     state.SetBytesProcessed(state.iterations());
493 }
494 
495 // Use the parameter list to send formatted output to a string
496 // string type
Bm_function_Vsprintf_str(benchmark::State & state)497 static void Bm_function_Vsprintf_str(benchmark::State &state)
498 {
499     const char *arr = "signal_stack";
500     for (auto _ : state) {
501         benchmark::DoNotOptimize(MyPrintfVs("%s", arr));
502     }
503 
504     state.SetBytesProcessed(state.iterations());
505 }
506 
507 // int type
Bm_function_Vsprintf_int(benchmark::State & state)508 static void Bm_function_Vsprintf_int(benchmark::State &state)
509 {
510     int i = 2233;
511     for (auto _ : state) {
512         benchmark::DoNotOptimize(MyPrintfVs("%d", i));
513     }
514 
515     state.SetBytesProcessed(state.iterations());
516 }
517 
518 // float type
Bm_function_Vsprintf_float(benchmark::State & state)519 static void Bm_function_Vsprintf_float(benchmark::State &state)
520 {
521     float i = 22.33;
522     for (auto _ : state) {
523         benchmark::DoNotOptimize(MyPrintfVs("%f", i));
524     }
525 
526     state.SetBytesProcessed(state.iterations());
527 }
528 
529 // longdouble type
Bm_function_Vsprintf_longdouble(benchmark::State & state)530 static void Bm_function_Vsprintf_longdouble(benchmark::State &state)
531 {
532     long double i = 9583454321234.226342465121L;
533     for (auto _ : state) {
534         benchmark::DoNotOptimize(MyPrintfVs("%Lf", i));
535     }
536 
537     state.SetBytesProcessed(state.iterations());
538 }
539 
540 // unsigned type
Bm_function_Vsprintf_unsigned(benchmark::State & state)541 static void Bm_function_Vsprintf_unsigned(benchmark::State &state)
542 {
543     unsigned int u = 4294967295U;
544     for (auto _ : state) {
545         benchmark::DoNotOptimize(MyPrintfVs("%u", u));
546     }
547 
548     state.SetBytesProcessed(state.iterations());
549 }
550 
551 // long type
Bm_function_Vsprintf_long(benchmark::State & state)552 static void Bm_function_Vsprintf_long(benchmark::State &state)
553 {
554     long l = 1234567890L;
555     for (auto _ : state) {
556         benchmark::DoNotOptimize(MyPrintfVs("%ld", l));
557     }
558 
559     state.SetBytesProcessed(state.iterations());
560 }
561 
562 // short type
Bm_function_Vsprintf_short(benchmark::State & state)563 static void Bm_function_Vsprintf_short(benchmark::State &state)
564 {
565     short s = 32767;
566     for (auto _ : state) {
567         benchmark::DoNotOptimize(MyPrintfVs("%hd", s));
568     }
569 
570     state.SetBytesProcessed(state.iterations());
571 }
572 
573 // char type
Bm_function_Vsprintf_char(benchmark::State & state)574 static void Bm_function_Vsprintf_char(benchmark::State &state)
575 {
576     char c = 'Z';
577     for (auto _ : state) {
578         benchmark::DoNotOptimize(MyPrintfVs("%c", c));
579     }
580 
581     state.SetBytesProcessed(state.iterations());
582 }
583 
584 // Prints a format string to a string buffer and can limit the maximum length of the formatted string that is printed
585 // string type
Bm_function_Vsnprintf_str(benchmark::State & state)586 static void Bm_function_Vsnprintf_str(benchmark::State &state)
587 {
588     const char *i = "holy";
589     for (auto _ : state) {
590         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %s", i));
591     }
592 
593     state.SetBytesProcessed(state.iterations());
594 }
595 
596 // int type
Bm_function_Vsnprintf_int(benchmark::State & state)597 static void Bm_function_Vsnprintf_int(benchmark::State &state)
598 {
599     int i = 2233;
600     for (auto _ : state) {
601         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %d", i));
602     }
603 
604     state.SetBytesProcessed(state.iterations());
605 }
606 
607 // float type
Bm_function_Vsnprintf_float(benchmark::State & state)608 static void Bm_function_Vsnprintf_float(benchmark::State &state)
609 {
610     float i = 22.33;
611     for (auto _ : state) {
612         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %f", i));
613     }
614 
615     state.SetBytesProcessed(state.iterations());
616 }
617 
618 // longdouble type
Bm_function_Vsnprintf_longdouble(benchmark::State & state)619 static void Bm_function_Vsnprintf_longdouble(benchmark::State &state)
620 {
621     long double i = 23423523.769563665L;
622     for (auto _ : state) {
623         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %Lf", i));
624     }
625 
626     state.SetBytesProcessed(state.iterations());
627 }
628 
629 // unsigned type
Bm_function_Vsnprintf_unsigned(benchmark::State & state)630 static void Bm_function_Vsnprintf_unsigned(benchmark::State &state)
631 {
632     unsigned int u = 4294967295U;
633     for (auto _ : state) {
634         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %u", u));
635     }
636 
637     state.SetBytesProcessed(state.iterations());
638 }
639 
640 // long type
Bm_function_Vsnprintf_long(benchmark::State & state)641 static void Bm_function_Vsnprintf_long(benchmark::State &state)
642 {
643     long l = 1234567890L;
644     for (auto _ : state) {
645         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %ld", l));
646     }
647 
648     state.SetBytesProcessed(state.iterations());
649 }
650 
651 // short type
Bm_function_Vsnprintf_short(benchmark::State & state)652 static void Bm_function_Vsnprintf_short(benchmark::State &state)
653 {
654     short s = 32767;
655     for (auto _ : state) {
656         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %hd", s));
657     }
658 
659     state.SetBytesProcessed(state.iterations());
660 }
661 
662 // char type
Bm_function_Vsnprintf_char(benchmark::State & state)663 static void Bm_function_Vsnprintf_char(benchmark::State &state)
664 {
665     char s = 'R';
666     for (auto _ : state) {
667         benchmark::DoNotOptimize(MyPrintfVsn("Error loading shared library %c", s));
668     }
669 
670     state.SetBytesProcessed(state.iterations());
671 }
672 
673 // Wait for the file to no longer be locked by another thread, then make the current thread the owner of the file
674 // and then increase the lock count and decrease the lock count
Bm_function_Flock_Funlockfile(benchmark::State & state)675 static void Bm_function_Flock_Funlockfile(benchmark::State &state)
676 {
677     FILE *fp = fopen("/dev/zero", "r");
678     if (fp == nullptr) {
679         perror("fopen funlockfile");
680     }
681     for (auto _ : state) {
682         flockfile(fp);
683         funlockfile(fp);
684     }
685     fclose(fp);
686     state.SetBytesProcessed(state.iterations());
687 }
688 
Bm_function_Flock(benchmark::State & state)689 static void Bm_function_Flock(benchmark::State &state)
690 {
691     int fd = open("/dev/zero", O_RDONLY);
692     if (fd == -1) {
693         perror("open flock");
694     }
695     for (auto _ : state) {
696         flock(fd, LOCK_EX);
697         flock(fd, LOCK_UN);
698     }
699     close(fd);
700     state.SetBytesProcessed(state.iterations());
701 }
702 
Bm_function_Rename(benchmark::State & state)703 static void Bm_function_Rename(benchmark::State &state)
704 {
705     mkdir(DATA_ROOT"/data/data/test_rename", S_IRWXU);
706     char oldname[32] = DATA_ROOT"/data/data/test_rename";
707     char newname[32] = "test_newname";
708     for (auto _ : state) {
709         benchmark::DoNotOptimize(rename(oldname, newname));
710     }
711     rmdir(newname);
712     state.SetBytesProcessed(state.iterations());
713 }
714 
Bm_function_Fseek_set(benchmark::State & state)715 static void Bm_function_Fseek_set(benchmark::State &state)
716 {
717     FILE *f = fopen("/dev/zero", "r");
718     if (f == nullptr) {
719         perror("fopen fseek set");
720     }
721     for (auto _ : state) {
722         benchmark::DoNotOptimize(fseek(f, 0, SEEK_SET));
723     }
724     fclose(f);
725     state.SetItemsProcessed(state.iterations());
726 }
727 
Bm_function_Fseeko_set(benchmark::State & state)728 static void Bm_function_Fseeko_set(benchmark::State &state)
729 {
730     FILE *f = fopen("/dev/zero", "r");
731     if (f == nullptr) {
732         perror("fopen fseeko set");
733     }
734     for (auto _ : state) {
735         benchmark::DoNotOptimize(fseeko(f, 0, SEEK_SET));
736     }
737     fclose(f);
738     state.SetItemsProcessed(state.iterations());
739 }
740 
741 constexpr int OFFSET_SIZE = 10L;
Bm_function_Fseek_cur(benchmark::State & state)742 static void Bm_function_Fseek_cur(benchmark::State &state)
743 {
744     FILE *f = fopen("/dev/zero", "r");
745     if (f == nullptr) {
746         perror("fopen fseek cur");
747     }
748     for (auto _ : state) {
749         benchmark::DoNotOptimize(fseek(f, OFFSET_SIZE, SEEK_CUR));
750     }
751     fclose(f);
752     state.SetItemsProcessed(state.iterations());
753 }
754 
Bm_function_Fseek_end(benchmark::State & state)755 static void Bm_function_Fseek_end(benchmark::State &state)
756 {
757     FILE *f = fopen("/dev/zero", "r");
758     if (f == nullptr) {
759         perror("fopen fseek end");
760     }
761     for (auto _ : state) {
762         benchmark::DoNotOptimize(fseek(f, -OFFSET_SIZE, SEEK_END));
763     }
764     fclose(f);
765     state.SetItemsProcessed(state.iterations());
766 }
767 
Bm_function_Fseeko_cur(benchmark::State & state)768 static void Bm_function_Fseeko_cur(benchmark::State &state)
769 {
770     FILE *f = fopen("/dev/zero", "r");
771     if (f == nullptr) {
772         perror("fopen fseeko cur");
773     }
774     for (auto _ : state) {
775         benchmark::DoNotOptimize(fseeko(f, OFFSET_SIZE, SEEK_CUR));
776     }
777     fclose(f);
778     state.SetItemsProcessed(state.iterations());
779 }
780 
Bm_function_Fseeko_end(benchmark::State & state)781 static void Bm_function_Fseeko_end(benchmark::State &state)
782 {
783     FILE *f = fopen("/dev/zero", "r");
784     if (f == nullptr) {
785         perror("fopen fseeko end");
786     }
787     for (auto _ : state) {
788         benchmark::DoNotOptimize(fseeko(f, -OFFSET_SIZE, SEEK_END));
789     }
790     fclose(f);
791     state.SetItemsProcessed(state.iterations());
792 }
793 
Bm_function_Sscanf_int(benchmark::State & state)794 static void Bm_function_Sscanf_int(benchmark::State &state)
795 {
796     for (auto _ : state) {
797         int year, month, day;
798         benchmark::DoNotOptimize(sscanf("20230515", "%04d%02d%02d", &year, &month, &day));
799     }
800     state.SetBytesProcessed(state.iterations());
801 }
802 
Bm_function_Sscanf_double(benchmark::State & state)803 static void Bm_function_Sscanf_double(benchmark::State &state)
804 {
805     double longitude;
806     double latitude;
807     for (auto _ : state) {
808         benchmark::DoNotOptimize(sscanf("113.123456789 31.123456789", "%lf %lf", &longitude, &latitude));
809     }
810     state.SetBytesProcessed(state.iterations());
811 }
812 
Bm_function_Sscanf_char(benchmark::State & state)813 static void Bm_function_Sscanf_char(benchmark::State &state)
814 {
815     for (auto _ : state) {
816         char str[SSCANF_SIZE] = "";
817         benchmark::DoNotOptimize(sscanf("123456abcdedf", "%31[0-9]", str));
818     }
819     state.SetBytesProcessed(state.iterations());
820 }
821 
Bm_function_Sscanf_char1(benchmark::State & state)822 static void Bm_function_Sscanf_char1(benchmark::State &state)
823 {
824     for (auto _ : state) {
825         char str[SSCANF_SIZE] = "";
826         benchmark::DoNotOptimize(sscanf("test/unique_11@qq.com", "%*[^/]/%[^@]", str));
827     }
828     state.SetBytesProcessed(state.iterations());
829 }
830 
Bm_function_Sscanf_str(benchmark::State & state)831 static void Bm_function_Sscanf_str(benchmark::State &state)
832 {
833     for (auto _ : state) {
834         char str[SSCANF_SIZE] = "";
835         benchmark::DoNotOptimize(sscanf("123456abcdedfBCDEF", "%[1-9A-Z]", str));
836     }
837     state.SetBytesProcessed(state.iterations());
838 }
839 
Bm_function_Sscanf_str1(benchmark::State & state)840 static void Bm_function_Sscanf_str1(benchmark::State &state)
841 {
842     for (auto _ : state) {
843         char str[SSCANF_SIZE] = "";
844         benchmark::DoNotOptimize(sscanf("test TEST", "%*s%s", str));
845     }
846     state.SetBytesProcessed(state.iterations());
847 }
848 
MyScanf1(FILE * stream,const char * format,...)849 int MyScanf1(FILE *stream, const char *format, ...)
850 {
851     va_list args;
852     int ret;
853     va_start(args, format);
854     ret = vfscanf(stream, format, args);
855     va_end(args);
856     return (ret);
857 }
858 
MyVfscanf(const char * str,const char * format,...)859 int MyVfscanf(const char* str, const char *format, ...)
860 {
861     va_list args;
862     int ret;
863     va_start(args, format);
864     ret = vsscanf(str, format, args);
865     va_end(args);
866     return (ret);
867 }
868 
Bm_function_Vfscanf_str(benchmark::State & state)869 static void Bm_function_Vfscanf_str(benchmark::State &state)
870 {
871     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_str.txt", "w+");
872     if (stream == nullptr) {
873         perror("fopen vfscanf str");
874     }
875     if (fprintf(stream, "%s", "vfscanfStrTest") < 0) {
876         perror("fprintf vfscanf str");
877     }
878     for (auto _ : state) {
879         char str[1024] = {'0'};
880         benchmark::DoNotOptimize(MyScanf1(stream, "%s", str));
881     }
882     fclose(stream);
883     remove(DATA_ROOT"/data/data/vfscanf_str.txt");
884     state.SetBytesProcessed(state.iterations());
885 }
886 
Bm_function_Vfscanf_int(benchmark::State & state)887 static void Bm_function_Vfscanf_int(benchmark::State &state)
888 {
889     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_int.txt", "w+");
890     if (stream == nullptr) {
891         perror("fopen vfscanf int");
892     }
893     int testNumber = 123;
894     if (fprintf(stream, "%d", testNumber) < 0) {
895         perror("fprintf vfscanf int");
896     }
897     for (auto _ : state) {
898         int val = 0;
899         benchmark::DoNotOptimize(MyScanf1(stream, "%d", &val));
900     }
901     fclose(stream);
902     remove(DATA_ROOT"/data/data/vfscanf_int.txt");
903     state.SetBytesProcessed(state.iterations());
904 }
905 
Bm_function_Vfscanf_double(benchmark::State & state)906 static void Bm_function_Vfscanf_double(benchmark::State &state)
907 {
908     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_double.txt", "w+");
909     if (stream == nullptr) {
910         perror("fopen vfscanf double");
911     }
912     double testNumber = 123.4567;
913     if (fprintf(stream, "%lf", testNumber) < 0) {
914         perror("fprintf vfscanf double");
915     }
916     for (auto _ : state) {
917         int val = 0;
918         benchmark::DoNotOptimize(MyScanf1(stream, "%d", &val));
919     }
920     fclose(stream);
921     remove(DATA_ROOT"/data/data/vfscanf_double.txt");
922     state.SetBytesProcessed(state.iterations());
923 }
924 
Bm_function_Vfscanf_float(benchmark::State & state)925 static void Bm_function_Vfscanf_float(benchmark::State &state)
926 {
927     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_float.txt", "w+");
928     if (stream == nullptr) {
929         perror("fopen vfscanf float");
930     }
931     double testNumber = 40.0;
932     if (fprintf(stream, "%f", testNumber) < 0) {
933         perror("fprintf vfscanf float");
934     }
935     for (auto _ : state) {
936         float val = 0.0;
937         benchmark::DoNotOptimize(MyScanf1(stream, "%f", &val));
938     }
939     fclose(stream);
940     remove(DATA_ROOT"/data/data/vfscanf_float.txt");
941     state.SetBytesProcessed(state.iterations());
942 }
943 
Bm_function_Vfscanf_char(benchmark::State & state)944 static void Bm_function_Vfscanf_char(benchmark::State &state)
945 {
946     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_char.txt", "w+");
947     if (stream == nullptr) {
948         perror("fopen vfscanf char");
949     }
950     if (fprintf(stream, "%c", 'a') < 0) {
951         perror("fprintf vfscanf char");
952     }
953     for (auto _ : state) {
954         char val = ' ';
955         benchmark::DoNotOptimize(MyScanf1(stream, "%c", &val));
956     }
957     fclose(stream);
958     remove(DATA_ROOT"/data/data/vfscanf_char.txt");
959     state.SetBytesProcessed(state.iterations());
960 }
961 
Bm_function_Vfscanf_iformat(benchmark::State & state)962 static void Bm_function_Vfscanf_iformat(benchmark::State &state)
963 {
964     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_iformat.txt", "w+");
965     if (stream == nullptr) {
966         perror("fopen vfscanf iformat");
967     }
968     if (fprintf(stream, "%i", -1) < 0) {
969         perror("fprintf vfscanf iformat");
970     }
971     for (auto _ : state) {
972         signed int val = 0;
973         benchmark::DoNotOptimize(MyScanf1(stream, "%i", &val));
974     }
975     fclose(stream);
976     remove(DATA_ROOT"/data/data/vfscanf_iformat.txt");
977     state.SetBytesProcessed(state.iterations());
978 }
979 
Bm_function_Vfscanf_oformat(benchmark::State & state)980 static void Bm_function_Vfscanf_oformat(benchmark::State &state)
981 {
982     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_oformat.txt", "w+");
983     if (stream == nullptr) {
984         perror("fopen vfscanf oformat");
985     }
986     int testNumber = 0123;
987     if (fprintf(stream, "%o", testNumber) < 0) {
988         perror("fprintf vfscanf oformat");
989     }
990     for (auto _ : state) {
991         unsigned int val = 0;
992         benchmark::DoNotOptimize(MyScanf1(stream, "%o", &val));
993     }
994     fclose(stream);
995     remove(DATA_ROOT"/data/data/vfscanf_oformat.txt");
996     state.SetBytesProcessed(state.iterations());
997 }
998 
Bm_function_Vfscanf_uformat(benchmark::State & state)999 static void Bm_function_Vfscanf_uformat(benchmark::State &state)
1000 {
1001     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_uformat.txt", "w+");
1002     if (stream == nullptr) {
1003         perror("fopen vfscanf uformat");
1004     }
1005     int testNumber = 1024;
1006     if (fprintf(stream, "%u", testNumber) < 0) {
1007         perror("fprintf vfscanf uformat");
1008     }
1009     for (auto _ : state) {
1010         unsigned int val = 0;
1011         benchmark::DoNotOptimize(MyScanf1(stream, "%u", &val));
1012     }
1013     fclose(stream);
1014     remove(DATA_ROOT"/data/data/vfscanf_uformat.txt");
1015     state.SetBytesProcessed(state.iterations());
1016 }
1017 
Bm_function_Vfscanf_xformat(benchmark::State & state)1018 static void Bm_function_Vfscanf_xformat(benchmark::State &state)
1019 {
1020     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_xformat.txt", "w+");
1021     if (stream == nullptr) {
1022         perror("fopen vfscanf xformat");
1023     }
1024     if (fprintf(stream, "%x", 0xabc) < 0) {
1025         perror("fprintf vfscanf xformat");
1026     }
1027     for (auto _ : state) {
1028         int val = 0x000;
1029         benchmark::DoNotOptimize(MyScanf1(stream, "%x", &val));
1030     }
1031     fclose(stream);
1032     remove(DATA_ROOT"/data/data/vfscanf_xformat.txt");
1033     state.SetBytesProcessed(state.iterations());
1034 }
1035 
Bm_function_Vfscanf_Xformat(benchmark::State & state)1036 static void Bm_function_Vfscanf_Xformat(benchmark::State &state)
1037 {
1038     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_Xformat.txt", "w+");
1039     if (stream == nullptr) {
1040         perror("fopen vfscanf Xformat");
1041     }
1042     if (fprintf(stream, "%X", 0xabc) < 0) {
1043         perror("fprintf vfscanf Xformat");
1044     }
1045     for (auto _ : state) {
1046         int val = 0x000;
1047         benchmark::DoNotOptimize(MyScanf1(stream, "%X", &val));
1048     }
1049     fclose(stream);
1050     remove(DATA_ROOT"/data/data/vfscanf_Xformat.txt");
1051     state.SetBytesProcessed(state.iterations());
1052 }
1053 
Bm_function_Vfscanf_eformat(benchmark::State & state)1054 static void Bm_function_Vfscanf_eformat(benchmark::State &state)
1055 {
1056     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_eformat.txt", "w+");
1057     if (stream == nullptr) {
1058         perror("fopen vfscanf eformat");
1059     }
1060     float testNumber = 123456.0;
1061     if (fprintf(stream, "%.2e", testNumber) < 0) {
1062         perror("fprintf vfscanf eformat");
1063     }
1064     for (auto _ : state) {
1065         float val = 0.0;
1066         benchmark::DoNotOptimize(MyScanf1(stream, "%.2e", &val));
1067     }
1068     fclose(stream);
1069     remove(DATA_ROOT"/data/data/vfscanf_eformat.txt");
1070     state.SetBytesProcessed(state.iterations());
1071 }
1072 
Bm_function_Vfscanf_gformat(benchmark::State & state)1073 static void Bm_function_Vfscanf_gformat(benchmark::State &state)
1074 {
1075     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_gformat.txt", "w+");
1076     if (stream == nullptr) {
1077         perror("fopen vfscanf gformat");
1078     }
1079     float testNumber = 12.3;
1080     if (fprintf(stream, "%g", testNumber) < 0) {
1081         perror("fprintf vfscanf gformat");
1082     }
1083     for (auto _ : state) {
1084         float val = 12.3;
1085         benchmark::DoNotOptimize(MyScanf1(stream, "%g", &val));
1086     }
1087     fclose(stream);
1088     remove(DATA_ROOT"/data/data/vfscanf_gformat.txt");
1089     state.SetBytesProcessed(state.iterations());
1090 }
1091 
Bm_function_Vfscanf_ldformat(benchmark::State & state)1092 static void Bm_function_Vfscanf_ldformat(benchmark::State &state)
1093 {
1094     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_gformat.txt", "w+");
1095     if (stream == nullptr) {
1096         perror("fopen vfscanf ldformat");
1097     }
1098     if (fprintf(stream, "%ld", 2147483646L) < 0) {
1099         perror("fprintf vfscanf ldformat");
1100     }
1101     for (auto _ : state) {
1102         long int val = 0;
1103         benchmark::DoNotOptimize(MyScanf1(stream, "%ld", &val));
1104     }
1105     fclose(stream);
1106     remove(DATA_ROOT"/data/data/vfscanf_ldformat.txt");
1107     state.SetBytesProcessed(state.iterations());
1108 }
1109 
Bm_function_Vfscanf_luformat(benchmark::State & state)1110 static void Bm_function_Vfscanf_luformat(benchmark::State &state)
1111 {
1112     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_luformat.txt", "w+");
1113     if (stream == nullptr) {
1114         perror("fopen vfscanf luformat");
1115     }
1116     if (fprintf(stream, "%lu", 4294967294UL) < 0) {
1117         perror("fprintf vfscanf luformat");
1118     }
1119     for (auto _ : state) {
1120         unsigned long val = 0UL;
1121         benchmark::DoNotOptimize(MyScanf1(stream, "%lu", &val));
1122     }
1123     fclose(stream);
1124     remove(DATA_ROOT"/data/data/vfscanf_luformat.txt");
1125     state.SetBytesProcessed(state.iterations());
1126 }
1127 
Bm_function_Vfscanf_lxformat(benchmark::State & state)1128 static void Bm_function_Vfscanf_lxformat(benchmark::State &state)
1129 {
1130     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_lxformat.txt", "w+");
1131     if (stream == nullptr) {
1132         perror("fopen vfscanf lxformat");
1133     }
1134     if (fprintf(stream, "%lx", 0x41L) < 0) {
1135         perror("fprintf vfscanf lxformat");
1136     }
1137     for (auto _ : state) {
1138         long val = 0x0L;
1139         benchmark::DoNotOptimize(MyScanf1(stream, "%lx", &val));
1140     }
1141     fclose(stream);
1142     remove(DATA_ROOT"/data/data/vfscanf_lxformat.txt");
1143     state.SetBytesProcessed(state.iterations());
1144 }
1145 
Bm_function_Vfscanf_loformat(benchmark::State & state)1146 static void Bm_function_Vfscanf_loformat(benchmark::State &state)
1147 {
1148     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_loformat.txt", "w+");
1149     if (stream == nullptr) {
1150         perror("fopen vfscanf loformat");
1151     }
1152     if (fprintf(stream, "%lo", 0234L) < 0) {
1153         perror("fprintf vfscanf loformat");
1154     }
1155     for (auto _ : state) {
1156         long val = 00L;
1157         benchmark::DoNotOptimize(MyScanf1(stream, "%lo", &val));
1158     }
1159     fclose(stream);
1160     remove(DATA_ROOT"/data/data/vfscanf_loformat.txt");
1161     state.SetBytesProcessed(state.iterations());
1162 }
1163 
Bm_function_Vfscanf_hdformat(benchmark::State & state)1164 static void Bm_function_Vfscanf_hdformat(benchmark::State &state)
1165 {
1166     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_hdformat.txt", "w+");
1167     if (stream == nullptr) {
1168         perror("fopen vfscanf hdformat");
1169     }
1170     if (fprintf(stream, "%hd", static_cast<short>(144)) < 0) {
1171         perror("fprintf vfscanf hdformat");
1172     }
1173     for (auto _ : state) {
1174         short int val = 0;
1175         benchmark::DoNotOptimize(MyScanf1(stream, "%hd", &val));
1176     }
1177     fclose(stream);
1178     remove(DATA_ROOT"/data/data/vfscanf_hdformat.txt");
1179     state.SetBytesProcessed(state.iterations());
1180 }
1181 
Bm_function_Vfscanf_huformat(benchmark::State & state)1182 static void Bm_function_Vfscanf_huformat(benchmark::State &state)
1183 {
1184     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_huformat.txt", "w+");
1185     if (stream == nullptr) {
1186         perror("fopen vfscanf huformat");
1187     }
1188     if (fprintf(stream, "%hu", static_cast<unsigned short>(256)) < 0) {
1189         perror("fprintf vfscanf huformat");
1190     }
1191     for (auto _ : state) {
1192         unsigned short int val = 0;
1193         benchmark::DoNotOptimize(MyScanf1(stream, "%hu", &val));
1194     }
1195     fclose(stream);
1196     remove(DATA_ROOT"/data/data/vfscanf_huformat.txt");
1197     state.SetBytesProcessed(state.iterations());
1198 }
1199 
Bm_function_Vfscanf_hhuformat(benchmark::State & state)1200 static void Bm_function_Vfscanf_hhuformat(benchmark::State &state)
1201 {
1202     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_hhuformat.txt", "w+");
1203     if (stream == nullptr) {
1204         perror("fopen vfscanf hhuformat");
1205     }
1206     if (fprintf(stream, "%hhu", static_cast<unsigned char>(256)) < 0) {
1207         perror("fprintf vfscanf hhuformat");
1208     }
1209     for (auto _ : state) {
1210         unsigned char val = 0;
1211         benchmark::DoNotOptimize(MyScanf1(stream, "%hhu", &val));
1212     }
1213     fclose(stream);
1214     remove(DATA_ROOT"/data/data/vfscanf_hhuformat.txt");
1215     state.SetBytesProcessed(state.iterations());
1216 }
1217 
Bm_function_Vfscanf_hhxformat(benchmark::State & state)1218 static void Bm_function_Vfscanf_hhxformat(benchmark::State &state)
1219 {
1220     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_hhxformat.txt", "w+");
1221     if (stream == nullptr) {
1222         perror("fopen vfscanf hhxformat");
1223     }
1224     if (fprintf(stream, "%hhx", static_cast<unsigned char>(0x23)) < 0) {
1225         perror("fprintf vfscanf hhxformat");
1226     }
1227     for (auto _ : state) {
1228         short val = 0x0;
1229         benchmark::DoNotOptimize(MyScanf1(stream, "%hhx", &val));
1230     }
1231     fclose(stream);
1232     remove(DATA_ROOT"/data/data/vfscanf_hhxformat.txt");
1233     state.SetBytesProcessed(state.iterations());
1234 }
1235 
Bm_function_Vfscanf_llxformat(benchmark::State & state)1236 static void Bm_function_Vfscanf_llxformat(benchmark::State &state)
1237 {
1238     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_llxformat.txt", "w+");
1239     if (stream == nullptr) {
1240         perror("fopen vfscanf llxformat");
1241     }
1242     if (fprintf(stream, "%llx", 0x6543LL) < 0) {
1243         perror("fprintf vfscanf llxformat");
1244     }
1245     for (auto _ : state) {
1246         long long val = 0x0LL;
1247         benchmark::DoNotOptimize(MyScanf1(stream, "%llx", &val));
1248     }
1249     fclose(stream);
1250     remove(DATA_ROOT"/data/data/vfscanf_llxformat.txt");
1251     state.SetBytesProcessed(state.iterations());
1252 }
1253 
Bm_function_Vfscanf_lldformat(benchmark::State & state)1254 static void Bm_function_Vfscanf_lldformat(benchmark::State &state)
1255 {
1256     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_lldformat.txt", "w+");
1257     if (stream == nullptr) {
1258         perror("fopen vfscanf lldformat");
1259     }
1260     if (fprintf(stream, "%lld", -23234534LL) < 0) {
1261         perror("fprintf vfscanf lldformat");
1262     }
1263     for (auto _ : state) {
1264         long long val = 0;
1265         benchmark::DoNotOptimize(MyScanf1(stream, "%lld", &val));
1266     }
1267     fclose(stream);
1268     remove(DATA_ROOT"/data/data/vfscanf_lldformat.txt");
1269     state.SetBytesProcessed(state.iterations());
1270 }
1271 
Bm_function_Vfscanf_lluformat(benchmark::State & state)1272 static void Bm_function_Vfscanf_lluformat(benchmark::State &state)
1273 {
1274     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_lluformat.txt", "w+");
1275     if (stream == nullptr) {
1276         perror("fopen vfscanf lluformat");
1277     }
1278     if (fprintf(stream, "%llu", 23234534LL) < 0) {
1279         perror("fprintf vfscanf lluformat");
1280     }
1281     for (auto _ : state) {
1282         unsigned long long val = 0;
1283         benchmark::DoNotOptimize(MyScanf1(stream, "%llu", &val));
1284     }
1285     fclose(stream);
1286     remove(DATA_ROOT"/data/data/vfscanf_lluformat.txt");
1287     state.SetBytesProcessed(state.iterations());
1288 }
1289 
Bm_function_Fileno_unlocked(benchmark::State & state)1290 static void Bm_function_Fileno_unlocked(benchmark::State &state)
1291 {
1292     FILE *stream = fopen(DATA_ROOT"/data/data/vfscanf_lluformat.txt", "w+");
1293     if (stream == nullptr) {
1294         perror("fopen vfscanf lluformat");
1295     }
1296     for (auto _ : state) {
1297         benchmark::DoNotOptimize(fileno_unlocked(stream));
1298     }
1299     fclose(stream);
1300     remove(DATA_ROOT"/data/data/vfscanf_lluformat.txt");
1301     state.SetBytesProcessed(state.iterations());
1302 }
1303 
Bm_function_Fseek_fflush(benchmark::State & state)1304 static void Bm_function_Fseek_fflush(benchmark::State &state)
1305 {
1306     FILE *f = fopen("/dev/zero", "r");
1307     if (f == nullptr) {
1308         perror("fopen fseek set");
1309     }
1310     for (auto _ : state) {
1311         benchmark::DoNotOptimize(fflush(f));
1312     }
1313     fclose(f);
1314     state.SetItemsProcessed(state.iterations());
1315 }
1316 
Bm_function_Sscanf_vsscanf_int(benchmark::State & state)1317 static void Bm_function_Sscanf_vsscanf_int(benchmark::State &state)
1318 {
1319     int year;
1320     int month;
1321     int day;
1322     const char* src = "20230515";
1323     for (auto _ : state) {
1324         benchmark::DoNotOptimize(MyVfscanf(src, "%04d%02d%02d", &year, &month, &day));
1325     }
1326 }
1327 
Bm_function_Feof(benchmark::State & state)1328 static void Bm_function_Feof(benchmark::State &state)
1329 {
1330     FILE *fp = fopen("/dev/zero", "r");
1331     if (fp == nullptr) {
1332         perror("feof");
1333     }
1334     for (auto _ : state) {
1335         benchmark::DoNotOptimize(feof(fp));
1336     }
1337     fclose(fp);
1338     state.SetBytesProcessed(state.iterations());
1339 }
1340 
1341 // Push the char character into the specified stream stream,
1342 // so that it is the next character to be read
Bm_function_Ungetc(benchmark::State & state)1343 static void Bm_function_Ungetc(benchmark::State &state)
1344 {
1345     int c;
1346     FILE *fp = fopen("/dev/zero", "r");
1347     if (fp == nullptr) {
1348         perror("ungetc open");
1349     }
1350     while (state.KeepRunning()) {
1351         c = fgetc(fp);
1352         ungetc(c, fp);
1353     }
1354     fclose(fp);
1355 }
1356 
Bm_function_Setbuf(benchmark::State & state)1357 static void Bm_function_Setbuf(benchmark::State &state)
1358 {
1359     FILE *stream = stdout;
1360 
1361     const size_t nbytes = state.range(0);
1362     const size_t bufAlignment = state.range(1);
1363 
1364     vector<char> src;
1365     char *bufAligned = GetAlignedPtr(&src, bufAlignment, nbytes);
1366     for (auto _ : state) {
1367         setbuf(stream, bufAligned);
1368     }
1369     setbuf(stream, nullptr);
1370 }
1371 
Bm_function_Getchar(benchmark::State & state)1372 static void Bm_function_Getchar(benchmark::State &state)
1373 {
1374     FILE *fp = freopen("/dev/zero", "r", stdin);
1375     for (auto _ : state) {
1376         benchmark::DoNotOptimize(getchar());
1377     }
1378     fclose(fp);
1379 }
1380 
Bm_function_Fputc(benchmark::State & state)1381 static void Bm_function_Fputc(benchmark::State &state)
1382 {
1383     FILE *fp = fopen("/dev/zero", "w+");
1384     if (fp == nullptr) {
1385         perror("fopen fputc");
1386     }
1387     char c = 'Z';
1388     for (auto _ : state) {
1389         benchmark::DoNotOptimize(fputc(c, fp));
1390     }
1391     fclose(fp);
1392     state.SetBytesProcessed(state.iterations());
1393 }
1394 
Bm_function_Fputs(benchmark::State & state)1395 static void Bm_function_Fputs(benchmark::State &state)
1396 {
1397     FILE *fp = fopen("/dev/zero", "w+");
1398     if (fp == nullptr) {
1399         perror("fopen fputs");
1400     }
1401     char str[BUFFERSIZE] = "asdhfdf";
1402     for (auto _ : state) {
1403         benchmark::DoNotOptimize(fputs(str, fp));
1404     }
1405     fclose(fp);
1406     state.SetBytesProcessed(state.iterations());
1407 }
1408 
1409 // Used to get the number of bytes offset from the beginning of the file
1410 // position pointer from the current position of the file
Bm_function_Ftell(benchmark::State & state)1411 static void Bm_function_Ftell(benchmark::State &state)
1412 {
1413     FILE* fp = fopen("/dev/zero", "w+");
1414     if (fp == nullptr) {
1415         perror("fopen ftell");
1416     }
1417     fseek(fp, 0, SEEK_END);
1418     for (auto _ : state) {
1419         benchmark::DoNotOptimize(ftell(fp));
1420     }
1421     fclose(fp);
1422     state.SetBytesProcessed(state.iterations());
1423 }
1424 #if not defined __APPLE__
Bm_function_fread_unlocked(benchmark::State & state)1425 static void Bm_function_fread_unlocked(benchmark::State& state)
1426 {
1427     FILE *fp = fopen("/dev/zero", "r");
1428     if (fp == nullptr) {
1429         perror("fopen read");
1430     }
1431     int n = 256;
1432     char* buf = new char[n];
1433     for (auto _ : state) {
1434         benchmark::DoNotOptimize(fread_unlocked(buf, n, 1, fp));
1435     }
1436     delete[] buf;
1437     fclose(fp);
1438 }
1439 
Bm_function_fgets_unlocked(benchmark::State & state)1440 static void Bm_function_fgets_unlocked(benchmark::State& state)
1441 {
1442     FILE *fp = fopen("/dev/zero", "r");
1443     if (fp == nullptr) {
1444         perror("fopen read");
1445     }
1446     int n = 256;
1447     char* buf = new char[n];
1448     for (auto _ : state) {
1449         benchmark::DoNotOptimize(fgets_unlocked(buf, n, fp));
1450     }
1451     delete[] buf;
1452     fclose(fp);
1453 }
1454 #endif
1455 
1456 // Rename files and directories
Bm_function_Renameat(benchmark::State & state)1457 static void Bm_function_Renameat(benchmark::State &state)
1458 {
1459     const char *oldPath = DATA_ROOT"/data/data/test_old_renameat.txt";
1460     int oldFd = open(oldPath, O_RDWR | O_CREAT, OPEN_MODE);
1461     if (oldFd == -1) {
1462         perror("open renameat old");
1463     }
1464     close(oldFd);
1465     const char *newPath = DATA_ROOT"/data/data/test_new_renameat.txt";
1466     int newFd = open(newPath, O_RDWR | O_CREAT, OPEN_MODE);
1467     if (newFd == -1) {
1468         perror("open renameat new");
1469     }
1470     close(newFd);
1471     for (auto _ : state) {
1472         benchmark::DoNotOptimize(renameat(oldFd, oldPath, newFd, newPath));
1473     }
1474     remove(newPath);
1475     state.SetBytesProcessed(state.iterations());
1476 }
1477 
BM_function_Snprintf_d4(benchmark::State & state)1478 static void BM_function_Snprintf_d4(benchmark::State& state)
1479 {
1480     char buf[BUFSIZ];
1481     char a[4] = {127, 0, 0, 1};
1482     while (state.KeepRunning()) {
1483         benchmark::DoNotOptimize(snprintf(buf, sizeof(buf), "%d.%d.%d.%d", a[0], a[1], a[2], a[3]));
1484     }
1485 }
1486 
1487 #if not defined __APPLE__
1488 MUSL_BENCHMARK(Bm_function_fgets_unlocked);
1489 MUSL_BENCHMARK(Bm_function_fread_unlocked);
1490 #endif
1491 MUSL_BENCHMARK(Bm_function_Fopen_read);
1492 MUSL_BENCHMARK(Bm_function_Fopen_write);
1493 MUSL_BENCHMARK(Bm_function_Fopen_append);
1494 MUSL_BENCHMARK(Bm_function_Fopen_rplus);
1495 MUSL_BENCHMARK(Bm_function_Fopen_wplus);
1496 MUSL_BENCHMARK(Bm_function_Fopen_append_plus);
1497 MUSL_BENCHMARK(Bm_function_Fopen_rb);
1498 MUSL_BENCHMARK(Bm_function_Fopen_wb);
1499 MUSL_BENCHMARK(Bm_function_Fopen_ab);
1500 MUSL_BENCHMARK(Bm_function_Fopen_rb_plus);
1501 MUSL_BENCHMARK(Bm_function_Fopen_wb_plus);
1502 MUSL_BENCHMARK(Bm_function_Fopen_ab_plus);
1503 MUSL_BENCHMARK(Bm_function_Fclose);
1504 MUSL_BENCHMARK(Bm_function_Fdopen);
1505 MUSL_BENCHMARK(Bm_function_Vfprintf_str);
1506 MUSL_BENCHMARK(Bm_function_Vfprintf_int);
1507 MUSL_BENCHMARK(Bm_function_Vfprintf_float);
1508 MUSL_BENCHMARK(Bm_function_Vfprintf_longdouble);
1509 MUSL_BENCHMARK(Bm_function_Vfprintf_unsigned);
1510 MUSL_BENCHMARK(Bm_function_Vfprintf_long);
1511 MUSL_BENCHMARK(Bm_function_Vfprintf_short);
1512 MUSL_BENCHMARK(Bm_function_Vfprintf_char);
1513 MUSL_BENCHMARK(Bm_function_Fprintf_str);
1514 MUSL_BENCHMARK(Bm_function_Fprintf_int);
1515 MUSL_BENCHMARK(Bm_function_Fprintf_float);
1516 MUSL_BENCHMARK(Bm_function_Fprintf_longdouble);
1517 MUSL_BENCHMARK(Bm_function_Fprintf_unsigned);
1518 MUSL_BENCHMARK(Bm_function_Fprintf_long);
1519 MUSL_BENCHMARK(Bm_function_Fprintf_short);
1520 MUSL_BENCHMARK(Bm_function_Fprintf_char);
1521 MUSL_BENCHMARK(Bm_function_Vsprintf_str);
1522 MUSL_BENCHMARK(Bm_function_Vsprintf_int);
1523 MUSL_BENCHMARK(Bm_function_Vsprintf_float);
1524 MUSL_BENCHMARK(Bm_function_Vsprintf_longdouble);
1525 MUSL_BENCHMARK(Bm_function_Vsprintf_unsigned);
1526 MUSL_BENCHMARK(Bm_function_Vsprintf_long);
1527 MUSL_BENCHMARK(Bm_function_Vsprintf_short);
1528 MUSL_BENCHMARK(Bm_function_Vsprintf_char);
1529 MUSL_BENCHMARK(Bm_function_Vsnprintf_str);
1530 MUSL_BENCHMARK(Bm_function_Vsnprintf_int);
1531 MUSL_BENCHMARK(Bm_function_Vsnprintf_float);
1532 MUSL_BENCHMARK(Bm_function_Vsnprintf_longdouble);
1533 MUSL_BENCHMARK(Bm_function_Vsnprintf_unsigned);
1534 MUSL_BENCHMARK(Bm_function_Vsnprintf_long);
1535 MUSL_BENCHMARK(Bm_function_Vsnprintf_short);
1536 MUSL_BENCHMARK(Bm_function_Vsnprintf_char);
1537 MUSL_BENCHMARK(Bm_function_Flock_Funlockfile);
1538 MUSL_BENCHMARK(Bm_function_Flock);
1539 MUSL_BENCHMARK(Bm_function_Rename);
1540 MUSL_BENCHMARK(Bm_function_Fseek_set);
1541 MUSL_BENCHMARK(Bm_function_Fseek_cur);
1542 MUSL_BENCHMARK(Bm_function_Fseek_end);
1543 MUSL_BENCHMARK(Bm_function_Fseeko_set);
1544 MUSL_BENCHMARK(Bm_function_Fseeko_cur);
1545 MUSL_BENCHMARK(Bm_function_Fseeko_end);
1546 MUSL_BENCHMARK(Bm_function_Sscanf_int);
1547 MUSL_BENCHMARK(Bm_function_Sscanf_double);
1548 MUSL_BENCHMARK(Bm_function_Sscanf_str);
1549 MUSL_BENCHMARK(Bm_function_Sscanf_str1);
1550 MUSL_BENCHMARK(Bm_function_Sscanf_char);
1551 MUSL_BENCHMARK(Bm_function_Sscanf_char1);
1552 MUSL_BENCHMARK(Bm_function_Vfscanf_str);
1553 MUSL_BENCHMARK(Bm_function_Vfscanf_int);
1554 MUSL_BENCHMARK(Bm_function_Vfscanf_double);
1555 MUSL_BENCHMARK(Bm_function_Vfscanf_float);
1556 MUSL_BENCHMARK(Bm_function_Vfscanf_char);
1557 MUSL_BENCHMARK(Bm_function_Vfscanf_iformat);
1558 MUSL_BENCHMARK(Bm_function_Vfscanf_oformat);
1559 MUSL_BENCHMARK(Bm_function_Vfscanf_uformat);
1560 MUSL_BENCHMARK(Bm_function_Vfscanf_xformat);
1561 MUSL_BENCHMARK(Bm_function_Vfscanf_Xformat);
1562 MUSL_BENCHMARK(Bm_function_Vfscanf_eformat);
1563 MUSL_BENCHMARK(Bm_function_Vfscanf_gformat);
1564 MUSL_BENCHMARK(Bm_function_Vfscanf_ldformat);
1565 MUSL_BENCHMARK(Bm_function_Vfscanf_luformat);
1566 MUSL_BENCHMARK(Bm_function_Vfscanf_lxformat);
1567 MUSL_BENCHMARK(Bm_function_Vfscanf_loformat);
1568 MUSL_BENCHMARK(Bm_function_Vfscanf_hdformat);
1569 MUSL_BENCHMARK(Bm_function_Vfscanf_huformat);
1570 MUSL_BENCHMARK(Bm_function_Vfscanf_hhuformat);
1571 MUSL_BENCHMARK(Bm_function_Vfscanf_hhxformat);
1572 MUSL_BENCHMARK(Bm_function_Vfscanf_llxformat);
1573 MUSL_BENCHMARK(Bm_function_Vfscanf_lldformat);
1574 MUSL_BENCHMARK(Bm_function_Vfscanf_lluformat);
1575 MUSL_BENCHMARK(Bm_function_Fileno_unlocked);
1576 MUSL_BENCHMARK(Bm_function_Fseek_fflush);
1577 MUSL_BENCHMARK(Bm_function_Sscanf_vsscanf_int);
1578 MUSL_BENCHMARK(Bm_function_Feof);
1579 MUSL_BENCHMARK(Bm_function_Ungetc);
1580 MUSL_BENCHMARK_WITH_ARG(Bm_function_Setbuf, "ALIGNED_ONEBUF");
1581 MUSL_BENCHMARK(Bm_function_Getchar);
1582 MUSL_BENCHMARK(Bm_function_Fputc);
1583 MUSL_BENCHMARK(Bm_function_Fputs);
1584 MUSL_BENCHMARK(Bm_function_Ftell);
1585 MUSL_BENCHMARK(Bm_function_Renameat);
1586 MUSL_BENCHMARK(BM_function_Snprintf_d4);
1587