op_parse_event.c
Go to the documentation of this file.00001
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <ctype.h>
00017
00018 #include "op_parse_event.h"
00019 #include "op_string.h"
00020 #include "op_events.h"
00021
00022 static char * next_part(char const ** str)
00023 {
00024 char const * c;
00025 char * ret;
00026
00027 if ((*str)[0] == '\0')
00028 return NULL;
00029
00030 if ((*str)[0] == ':')
00031 ++(*str);
00032
00033 c = *str;
00034
00035 while (*c != '\0' && *c != ':')
00036 ++c;
00037
00038 if (c == *str)
00039 return NULL;
00040
00041 ret = op_xstrndup(*str, c - *str);
00042 *str += c - *str;
00043 return ret;
00044 }
00045
00046
00047 static int parse_ulong(char const * str)
00048 {
00049 unsigned long value;
00050 char * end;
00051 value = strtoul(str, &end, 0);
00052 if (end && *end) {
00053 fprintf(stderr, "Invalid event part %s\n", str);
00054 exit(EXIT_FAILURE);
00055 }
00056
00057 return value;
00058 }
00059
00060
00061 size_t parse_events(struct parsed_event * parsed_events, size_t max_events,
00062 char const * const * events)
00063 {
00064 size_t i = 0;
00065 int timer_event_found_p = 0;
00066
00067 while (events[i]) {
00068 char const * cp = events[i];
00069 char * part = next_part(&cp);
00070
00071 if (i >= max_events) {
00072 fprintf(stderr, "Too many events specified: CPU "
00073 "only has %lu counters.\n",
00074 (unsigned long) max_events);
00075 exit(EXIT_FAILURE);
00076 }
00077
00078 if (!part) {
00079 fprintf(stderr, "Invalid event %s\n", cp);
00080 exit(EXIT_FAILURE);
00081 }
00082
00083 if (strcmp(part, TIMER_EVENT_NAME) == 0)
00084 timer_event_found_p = 1;
00085
00086 parsed_events[i].name = part;
00087
00088 part = next_part(&cp);
00089
00090 if (!part) {
00091 fprintf(stderr, "Invalid count for event %s\n", events[i]);
00092 exit(EXIT_FAILURE);
00093 }
00094
00095 parsed_events[i].count = parse_ulong(part);
00096 free(part);
00097
00098 parsed_events[i].unit_mask = 0;
00099 part = next_part(&cp);
00100
00101 if (part) {
00102 parsed_events[i].unit_mask_valid = 1;
00103 if (!isdigit(*part))
00104 parsed_events[i].unit_mask_name = part;
00105 else {
00106 parsed_events[i].unit_mask = parse_ulong(part);
00107 free(part);
00108 }
00109 }
00110
00111 parsed_events[i].kernel = 1;
00112 part = next_part(&cp);
00113
00114 if (part) {
00115 parsed_events[i].kernel = parse_ulong(part);
00116 free(part);
00117 }
00118
00119 parsed_events[i].user = 1;
00120 part = next_part(&cp);
00121
00122 if (part) {
00123 parsed_events[i].user = parse_ulong(part);
00124 free(part);
00125 }
00126
00127 ++i;
00128 }
00129
00130 if (i > 1 && timer_event_found_p) {
00131 fprintf(stderr, "TIMER event cannot be used in combination with"
00132 " hardware counters.\n");
00133 exit(EXIT_FAILURE);
00134 }
00135
00136 return i;
00137 }