Changeset 3bd74758 in mainline for uspace/app/perf/malloc
- Timestamp:
- 2018-12-28T09:32:11Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c7de81b
- Parents:
- 8ee106b
- Location:
- uspace/app/perf/malloc
- Files:
-
- 4 edited
-
malloc1.c (modified) (1 diff)
-
malloc1.def (modified) (1 diff)
-
malloc2.c (modified) (1 diff)
-
malloc2.def (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/perf/malloc/malloc1.c
r8ee106b r3bd74758 30 30 #include <stdio.h> 31 31 #include <stdlib.h> 32 #include <time.h>33 #include <errno.h>34 32 #include "../perf.h" 35 33 36 #define MIN_DURATION_SECS 10 37 #define NUM_SAMPLES 10 38 39 static errno_t malloc1_measure(uint64_t niter, uint64_t *rduration) 34 bool bench_malloc1(stopwatch_t *stopwatch, uint64_t size, 35 char *error, size_t error_size) 40 36 { 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; 37 stopwatch_start(stopwatch); 38 for (uint64_t i = 0; i < size; i++) { 39 void *p = malloc(1); 40 if (p == NULL) { 41 snprintf(error, error_size, 42 "failed to allocate 1B in run %" PRIu64 " (out of %" PRIu64 ")", 43 i, size); 44 return false; 45 } 51 46 free(p); 52 47 } 48 stopwatch_stop(stopwatch); 53 49 54 struct timespec now; 55 getuptime(&now); 56 57 *rduration = ts_sub_diff(&now, &start) / 1000; 58 return EOK; 50 return true; 59 51 } 60 61 static 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 73 const 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 sqrt131 132 printf("Average: %.0f cycles/s Std.dev^2: %.0f cycles/s Samples: %d\n",133 avg, stddev, NUM_SAMPLES);134 135 return NULL;136 error:137 return msg;138 } -
uspace/app/perf/malloc/malloc1.def
r8ee106b r3bd74758 2 2 "malloc1", 3 3 "User-space memory allocator benchmark, repeatedly allocate one block", 4 &bench_malloc1 4 &bench_malloc1, 5 NULL, NULL 5 6 }, -
uspace/app/perf/malloc/malloc2.c
r8ee106b r3bd74758 27 27 */ 28 28 29 #include < math.h>29 #include <stdlib.h> 30 30 #include <stdio.h> 31 #include <stdlib.h>32 #include <time.h>33 #include <errno.h>34 31 #include "../perf.h" 35 32 36 #define MIN_DURATION_SECS 10 37 #define NUM_SAMPLES 10 33 bool bench_malloc2(stopwatch_t *stopwatch, uint64_t niter, 34 char *error, size_t error_size) 35 { 36 stopwatch_start(stopwatch); 38 37 39 static 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; 38 void **p = malloc(niter * sizeof(void *)); 39 if (p == NULL) { 40 snprintf(error, error_size, 41 "failed to allocate backend array (%" PRIu64 "B)", 42 niter * sizeof(void *)); 43 return false; 55 44 } 56 45 57 for (count = 0; count < niter; count++) 46 for (uint64_t count = 0; count < niter; count++) { 47 p[count] = malloc(1); 48 if (p[count] == NULL) { 49 snprintf(error, error_size, 50 "failed to allocate 1B in run %" PRIu64 " (out of %" PRIu64 ")", 51 count, niter); 52 for (uint64_t j = 0; j < count; j++) { 53 free(p[j]); 54 } 55 free(p); 56 return false; 57 } 58 } 59 60 for (uint64_t count = 0; count < niter; count++) 58 61 free(p[count]); 59 62 60 63 free(p); 61 64 62 struct timespec now; 63 getuptime(&now); 65 stopwatch_stop(stopwatch); 64 66 65 *rduration = ts_sub_diff(&now, &start) / 1000; 66 return EOK; 67 return true; 67 68 } 68 69 static 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 81 const 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 sqrt139 140 printf("Average: %.0f cycles/s Std.dev^2: %.0f cycles/s Samples: %d\n",141 avg, stddev, NUM_SAMPLES);142 143 return NULL;144 error:145 return msg;146 } -
uspace/app/perf/malloc/malloc2.def
r8ee106b r3bd74758 2 2 "malloc2", 3 3 "User-space memory allocator benchmark, allocate many small blocks", 4 &bench_malloc2 4 &bench_malloc2, 5 NULL, NULL 5 6 },
Note:
See TracChangeset
for help on using the changeset viewer.
