profile-aggregates.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "sys/profile.h"
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 struct aggregate {
00047 const char *ptr;
00048 unsigned short episodes;
00049 unsigned long cycles;
00050 };
00051
00052 #define DETAILED_AGGREGATES 0
00053
00054 #define MAX_CATEGORIES 32
00055 #define LIST_LEN 100
00056
00057 static struct aggregate aggregates[LIST_LEN];
00058
00059 static int aggregates_list_ptr = 0;
00060
00061
00062 static struct aggregate *
00063 find_aggregate_category(const uint16_t cat)
00064 {
00065 int i;
00066 uint16_t acat;
00067
00068
00069
00070
00071 for(i = 0; i < aggregates_list_ptr; ++i) {
00072 acat = (aggregates[i].ptr[0] << 8) + aggregates[i].ptr[1];
00073
00074
00075
00076
00077 if(acat == cat) {
00078 return &aggregates[i];
00079 }
00080 }
00081
00082 if(i == LIST_LEN) {
00083 return NULL;
00084 }
00085
00086 aggregates[aggregates_list_ptr].ptr = NULL;
00087 return &aggregates[aggregates_list_ptr++];
00088 }
00089
00090 #if DETAILED_AGGREGATES
00091 static struct aggregate *
00092 find_aggregate(const unsigned char *ptr)
00093 {
00094 int i;
00095 for(i = 0; i < aggregates_list_ptr; ++i) {
00096 if(aggregates[i].ptr == ptr) {
00097 return &aggregates[i];
00098 }
00099 }
00100 if(i == LIST_LEN) {
00101 return NULL;
00102 }
00103
00104 return &aggregates[aggregates_list_ptr++];
00105 }
00106 #endif
00107
00108 void
00109 profile_aggregates_print(void)
00110 {
00111 int i;
00112
00113 #if DETAILED_AGGREGATES
00114 for(i = 0; i < aggregates_list_ptr; ++i) {
00115 printf("-- %s: %lu / %u = %lu\n", aggregates[i].ptr,
00116 aggregates[i].cycles,
00117 aggregates[i].episodes,
00118 aggregates[i].cycles / aggregates[i].episodes);
00119 }
00120 #else
00121 for(i = 0; i < aggregates_list_ptr; ++i) {
00122 printf("-- %c%c: %lu / %u = %lu\n",
00123 aggregates[i].ptr[0], aggregates[i].ptr[1],
00124 aggregates[i].cycles,
00125 aggregates[i].episodes,
00126 aggregates[i].cycles / aggregates[i].episodes);
00127 }
00128 #endif
00129
00130 printf("Memory for aggregates: %d * %d = %d\n",
00131 (int)sizeof(struct aggregate), aggregates_list_ptr,
00132 (int)sizeof(struct aggregate) * aggregates_list_ptr);
00133 }
00134
00135 #if DETAILED_AGGREGATES
00136 static void
00137 detailed_profile_aggregates_compute(void)
00138 {
00139 int i;
00140 rtimer_clock_t t;
00141
00142
00143
00144
00145 t = profile_timestamps[0].time;
00146
00147 for(i = 1; i < PROFILE_TIMESTAMP_PTR; ++i) {
00148 struct aggregate *a;
00149 a = find_aggregate(profile_timestamps[i - 1].ptr);
00150 if(a == NULL) {
00151
00152 printf("profile_aggregates_compute: list full\n");
00153 } else if(a->ptr == NULL) {
00154 a->ptr = profile_timestamps[i - 1].ptr;
00155 a->cycles = (unsigned long)(profile_timestamps[i].time - t);
00156 a->episodes = 1;
00157 } else {
00158 a->cycles += (unsigned long)(profile_timestamps[i].time - t);
00159 a->episodes++;
00160 }
00161 t = profile_timestamps[i].time;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 }
00173 #endif
00174
00175 static void
00176 category_profile_aggregates_compute(void)
00177 {
00178 int i,j;
00179 rtimer_clock_t t;
00180 uint16_t categories[MAX_CATEGORIES];
00181 int categories_ptr = 0;
00182
00183
00184
00185
00186 t = profile_timestamps[0].time;
00187
00188 for(i = 1; i < PROFILE_TIMESTAMP_PTR; ++i) {
00189 struct aggregate *a;
00190 uint16_t cat;
00191
00192
00193
00194 cat = (profile_timestamps[i - 1].ptr[0] << 8) +
00195 (profile_timestamps[i - 1].ptr[1] & 0xff);
00196 a = find_aggregate_category(cat);
00197 if(a == NULL) {
00198
00199 printf("profile_aggregates_compute: list full\n");
00200 } else if(a->ptr == NULL) {
00201 a->ptr = profile_timestamps[i - 1].ptr;
00202 a->cycles = (unsigned long)(profile_timestamps[i].time - t - profile_timestamp_time);
00203 a->episodes = 1;
00204 } else {
00205
00206 a->cycles += (unsigned long)(profile_timestamps[i].time - t - profile_timestamp_time);
00207
00208
00209
00210
00211
00212
00213
00214
00215 for(j = 0; j < categories_ptr; ++j) {
00216 if(categories[j] == cat) {
00217 break;
00218 }
00219 }
00220 if(j == categories_ptr) {
00221 categories[j] = cat;
00222 categories_ptr++;
00223 a->episodes++;
00224 }
00225 }
00226 t = profile_timestamps[i].time;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 }
00238
00239 void
00240 profile_aggregates_compute(void)
00241 {
00242 #if DETAILED_AGGREGATES
00243 detailed_profile_aggregates_compute();
00244 #else
00245 category_profile_aggregates_compute();
00246 #endif
00247 }
00248