source: mainline/uspace/app/perf/malloc/malloc2.c@ 02c6dcc

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

Two simple memory allocation benchmarks.

  • Property mode set to 100644
File size: 3.5 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 malloc2_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 p = malloc(niter * sizeof(void *));
48 if (p == NULL)
49 return ENOMEM;
50
51 for (count = 0; count < niter; count++) {
52 p[count] = malloc(1);
53 if (p[count] == NULL)
54 return ENOMEM;
55 }
56
57 for (count = 0; count < niter; count++)
58 free(p[count]);
59
60 free(p);
61
62 struct timespec now;
63 getuptime(&now);
64
65 *rduration = ts_sub_diff(&now, &start) / 1000;
66 return EOK;
67}
68
69static void malloc2_report(uint64_t niter, uint64_t duration)
70{
71 printf("Completed %" PRIu64 " allocations and deallocations in %" PRIu64 " us",
72 niter, duration);
73
74 if (duration > 0) {
75 printf(", %" PRIu64 " cycles/s.\n", niter * 1000 * 1000 / duration);
76 } else {
77 printf(".\n");
78 }
79}
80
81const char *bench_malloc2(void)
82{
83 errno_t rc;
84 uint64_t duration;
85 uint64_t dsmp[NUM_SAMPLES];
86 const char *msg;
87
88 printf("Warm up and determine work size...\n");
89
90 struct timespec start;
91 getuptime(&start);
92
93 uint64_t niter = 1;
94
95 while (true) {
96 rc = malloc2_measure(niter, &duration);
97 if (rc != EOK) {
98 msg = "Failed.";
99 goto error;
100 }
101
102 malloc2_report(niter, duration);
103
104 if (duration >= MIN_DURATION_SECS * 1000000)
105 break;
106
107 niter *= 2;
108 }
109
110 printf("Measure %d samples...\n", NUM_SAMPLES);
111
112 int i;
113
114 for (i = 0; i < NUM_SAMPLES; i++) {
115 rc = malloc2_measure(niter, &dsmp[i]);
116 if (rc != EOK) {
117 msg = "Failed.";
118 goto error;
119 }
120
121 malloc2_report(niter, dsmp[i]);
122 }
123
124 double sum = 0.0;
125
126 for (i = 0; i < NUM_SAMPLES; i++)
127 sum += (double)niter / ((double)dsmp[i] / 1000000.0l);
128
129 double avg = sum / NUM_SAMPLES;
130
131 double qd = 0.0;
132 double d;
133 for (i = 0; i < NUM_SAMPLES; i++) {
134 d = (double)niter / ((double)dsmp[i] / 1000000.0l) - avg;
135 qd += d * d;
136 }
137
138 double stddev = qd / (NUM_SAMPLES - 1); // XXX sqrt
139
140 printf("Average: %.0f cycles/s Std.dev^2: %.0f cycles/s Samples: %d\n",
141 avg, stddev, NUM_SAMPLES);
142
143 return NULL;
144error:
145 return msg;
146}
Note: See TracBrowser for help on using the repository browser.