source: mainline/uspace/app/perf/malloc/malloc1.c@ d4ceba0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4ceba0 was e131833c, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Two simple memory allocation benchmarks.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <math.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <time.h>
33#include <errno.h>
34#include "../perf.h"
35
36#define MIN_DURATION_SECS 10
37#define NUM_SAMPLES 10
38
39static errno_t malloc1_measure(uint64_t niter, uint64_t *rduration)
40{
41 struct timespec start;
42 uint64_t count;
43 void *p;
44
45 getuptime(&start);
46
47 for (count = 0; count < niter; count++) {
48 p = malloc(1);
49 if (p == NULL)
50 return ENOMEM;
51 free(p);
52 }
53
54 struct timespec now;
55 getuptime(&now);
56
57 *rduration = ts_sub_diff(&now, &start) / 1000;
58 return EOK;
59}
60
61static void malloc1_report(uint64_t niter, uint64_t duration)
62{
63 printf("Completed %" PRIu64 " allocations and deallocations in %" PRIu64 " us",
64 niter, duration);
65
66 if (duration > 0) {
67 printf(", %" PRIu64 " cycles/s.\n", niter * 1000 * 1000 / duration);
68 } else {
69 printf(".\n");
70 }
71}
72
73const char *bench_malloc1(void)
74{
75 errno_t rc;
76 uint64_t duration;
77 uint64_t dsmp[NUM_SAMPLES];
78 const char *msg;
79
80 printf("Warm up and determine work size...\n");
81
82 struct timespec start;
83 getuptime(&start);
84
85 uint64_t niter = 1;
86
87 while (true) {
88 rc = malloc1_measure(niter, &duration);
89 if (rc != EOK) {
90 msg = "Failed.";
91 goto error;
92 }
93
94 malloc1_report(niter, duration);
95
96 if (duration >= MIN_DURATION_SECS * 1000000)
97 break;
98
99 niter *= 2;
100 }
101
102 printf("Measure %d samples...\n", NUM_SAMPLES);
103
104 int i;
105
106 for (i = 0; i < NUM_SAMPLES; i++) {
107 rc = malloc1_measure(niter, &dsmp[i]);
108 if (rc != EOK) {
109 msg = "Failed.";
110 goto error;
111 }
112
113 malloc1_report(niter, dsmp[i]);
114 }
115
116 double sum = 0.0;
117
118 for (i = 0; i < NUM_SAMPLES; i++)
119 sum += (double)niter / ((double)dsmp[i] / 1000000.0l);
120
121 double avg = sum / NUM_SAMPLES;
122
123 double qd = 0.0;
124 double d;
125 for (i = 0; i < NUM_SAMPLES; i++) {
126 d = (double)niter / ((double)dsmp[i] / 1000000.0l) - avg;
127 qd += d * d;
128 }
129
130 double stddev = qd / (NUM_SAMPLES - 1); // XXX sqrt
131
132 printf("Average: %.0f cycles/s Std.dev^2: %.0f cycles/s Samples: %d\n",
133 avg, stddev, NUM_SAMPLES);
134
135 return NULL;
136error:
137 return msg;
138}
Note: See TracBrowser for help on using the repository browser.