Antonello Dettori | 4b1668f | 2016-07-08 11:14:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreinfo project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include "coreinfo.h" |
| 15 | #include <commonlib/timestamp_serialized.h> |
| 16 | |
| 17 | #if IS_ENABLED(CONFIG_MODULE_TIMESTAMPS) |
| 18 | |
| 19 | #define LINES_SHOWN 19 |
| 20 | #define TAB_WIDTH 2 |
| 21 | |
| 22 | /* Globals that are used for tracking screen state */ |
| 23 | static char *g_buf; |
| 24 | static s32 g_line; |
| 25 | static s32 g_lines_count; |
| 26 | static s32 g_max_cursor_line; |
| 27 | |
| 28 | static unsigned long tick_freq_mhz; |
| 29 | |
| 30 | static const char *timestamp_name(uint32_t id) |
| 31 | { |
| 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) { |
| 35 | if (timestamp_ids[i].id == id) |
| 36 | return timestamp_ids[i].name; |
| 37 | } |
| 38 | |
| 39 | return "<unknown>"; |
| 40 | } |
| 41 | |
| 42 | static void timestamp_set_tick_freq(unsigned long table_tick_freq_mhz) |
| 43 | { |
| 44 | tick_freq_mhz = table_tick_freq_mhz; |
| 45 | |
| 46 | /* Honor table frequency. */ |
| 47 | if (tick_freq_mhz) |
| 48 | return; |
| 49 | |
| 50 | tick_freq_mhz = lib_sysinfo.cpu_khz / 1000; |
| 51 | |
| 52 | if (!tick_freq_mhz) { |
| 53 | fprintf(stderr, "Cannot determine timestamp tick frequency.\n"); |
| 54 | exit(1); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static u64 arch_convert_raw_ts_entry(u64 ts) |
| 59 | { |
| 60 | return ts / tick_freq_mhz; |
| 61 | } |
| 62 | |
| 63 | static u32 char_width(char c, u32 cursor, u32 screen_width) |
| 64 | { |
| 65 | if (c == '\n') |
| 66 | return screen_width - (cursor % screen_width); |
| 67 | else if (c == '\t') |
| 68 | return TAB_WIDTH; |
| 69 | else if (isprint(c)) |
| 70 | return 1; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static u32 calculate_chars_count(char *str, u32 str_len, u32 screen_width, |
| 76 | u32 screen_height) |
| 77 | { |
| 78 | u32 i, count = 0; |
| 79 | |
| 80 | for (i = 0; i < str_len; i++) |
| 81 | count += char_width(str[i], count, screen_width); |
| 82 | |
| 83 | /* Ensure that 'count' can occupy at least the whole screen */ |
| 84 | if (count < screen_width * screen_height) |
| 85 | count = screen_width * screen_height; |
| 86 | |
| 87 | /* Pad to line end */ |
| 88 | if (count % screen_width != 0) |
| 89 | count += screen_width - (count % screen_width); |
| 90 | |
| 91 | return count; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * This method takes an input buffer and sanitizes it for display, which means: |
| 96 | * - '\n' is converted to spaces until end of line |
| 97 | * - Tabs are converted to spaces of size TAB_WIDTH |
| 98 | * - Only printable characters are preserved |
| 99 | */ |
| 100 | static int sanitize_buffer_for_display(char *str, u32 str_len, char *out, |
| 101 | u32 out_len, u32 screen_width) |
| 102 | { |
| 103 | u32 cursor = 0; |
| 104 | u32 i; |
| 105 | |
| 106 | for (i = 0; i < str_len && cursor < out_len; i++) { |
| 107 | u32 width = char_width(str[i], cursor, screen_width); |
| 108 | |
| 109 | if (width == 1) |
| 110 | out[cursor++] = str[i]; |
| 111 | else if (width > 1) |
| 112 | while (width-- && cursor < out_len) |
| 113 | out[cursor++] = ' '; |
| 114 | } |
| 115 | |
| 116 | /* Fill the rest of the out buffer with spaces */ |
| 117 | while (cursor < out_len) |
| 118 | out[cursor++] = ' '; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static uint64_t timestamp_print_entry(char *buffer, size_t size, uint32_t *cur, |
| 124 | uint32_t id, uint64_t stamp, uint64_t prev_stamp) |
| 125 | { |
| 126 | const char *name; |
| 127 | uint64_t step_time; |
| 128 | |
| 129 | name = timestamp_name(id); |
| 130 | step_time = arch_convert_raw_ts_entry(stamp - prev_stamp); |
| 131 | |
| 132 | *cur += snprintf(buffer + *cur, size, "%4d: %-45s", id, name); |
| 133 | *cur += snprintf(buffer + *cur, size, "%llu", |
| 134 | arch_convert_raw_ts_entry(stamp)); |
| 135 | if (prev_stamp) { |
| 136 | *cur += snprintf(buffer + *cur, size, " ("); |
| 137 | *cur += snprintf(buffer + *cur, size, "%llu", step_time); |
| 138 | *cur += snprintf(buffer + *cur, size, ")"); |
| 139 | } |
| 140 | *cur += snprintf(buffer + *cur, size, "\n"); |
| 141 | |
| 142 | return step_time; |
| 143 | } |
| 144 | |
| 145 | static int timestamps_module_init(void) |
| 146 | { |
| 147 | /* Make sure that lib_sysinfo is initialized */ |
| 148 | int ret = lib_get_sysinfo(); |
| 149 | |
| 150 | if (ret) |
| 151 | return -1; |
| 152 | |
| 153 | struct timestamp_table *timestamps = lib_sysinfo.tstamp_table; |
| 154 | |
| 155 | if (timestamps == NULL) |
| 156 | return -1; |
| 157 | |
| 158 | /* Extract timestamps information */ |
| 159 | u64 base_time = timestamps->base_time; |
| 160 | u16 max_entries = timestamps->max_entries; |
| 161 | u32 n_entries = timestamps->num_entries; |
| 162 | |
| 163 | timestamp_set_tick_freq(timestamps->tick_freq_mhz); |
| 164 | |
| 165 | char *buffer; |
| 166 | u32 buff_cur = 0; |
| 167 | uint64_t prev_stamp; |
| 168 | uint64_t total_time; |
| 169 | |
| 170 | /* Allocate a buffer big enough to contain all of the possible |
| 171 | * entries plus the other information (number entries, total time). */ |
| 172 | buffer = malloc((max_entries + 4) * SCREEN_X * sizeof(char)); |
| 173 | |
| 174 | if (buffer == NULL) |
| 175 | return -3; |
| 176 | |
| 177 | /* Write the content */ |
| 178 | buff_cur += snprintf(buffer, SCREEN_X, "%d entries total:\n\n", |
| 179 | n_entries); |
| 180 | |
| 181 | prev_stamp = 0; |
| 182 | timestamp_print_entry(buffer, SCREEN_X, &buff_cur, 0, base_time, |
| 183 | prev_stamp); |
| 184 | prev_stamp = base_time; |
| 185 | |
| 186 | total_time = 0; |
| 187 | for (int i = 0; i < n_entries; i++) { |
| 188 | uint64_t stamp; |
| 189 | const struct timestamp_entry *tse = ×tamps->entries[i]; |
| 190 | |
| 191 | stamp = tse->entry_stamp + base_time; |
| 192 | total_time += timestamp_print_entry(buffer, SCREEN_X, |
| 193 | &buff_cur, tse->entry_id, stamp, prev_stamp); |
| 194 | prev_stamp = stamp; |
| 195 | } |
| 196 | |
| 197 | buff_cur += snprintf(buffer + buff_cur, SCREEN_X, "\nTotal Time: "); |
| 198 | buff_cur += snprintf(buffer + buff_cur, SCREEN_X, "%llu", total_time); |
| 199 | buff_cur += snprintf(buffer + buff_cur, SCREEN_X, "\n"); |
| 200 | |
| 201 | /* Calculate how many characters will be displayed on screen */ |
| 202 | u32 chars_count = calculate_chars_count(buffer, buff_cur + 1, |
| 203 | SCREEN_X, LINES_SHOWN); |
| 204 | |
| 205 | /* Sanity check, chars_count must be padded to full line */ |
| 206 | if (chars_count % SCREEN_X != 0) |
| 207 | return -2; |
| 208 | |
| 209 | g_lines_count = chars_count / SCREEN_X; |
| 210 | g_max_cursor_line = MAX(g_lines_count - 1 - LINES_SHOWN, 0); |
| 211 | |
| 212 | g_buf = malloc(chars_count); |
Martin Roth | 2d1e0eb | 2017-08-03 10:55:01 -0600 | [diff] [blame] | 213 | if (!g_buf) { |
| 214 | free(buffer); |
Antonello Dettori | 4b1668f | 2016-07-08 11:14:40 +0200 | [diff] [blame] | 215 | return -3; |
Martin Roth | 2d1e0eb | 2017-08-03 10:55:01 -0600 | [diff] [blame] | 216 | } |
Antonello Dettori | 4b1668f | 2016-07-08 11:14:40 +0200 | [diff] [blame] | 217 | |
| 218 | if (sanitize_buffer_for_display(buffer, buff_cur + 1, g_buf, |
| 219 | chars_count, SCREEN_X) < 0) { |
| 220 | free(buffer); |
| 221 | free(g_buf); |
| 222 | g_buf = NULL; |
| 223 | return -4; |
| 224 | } |
| 225 | |
| 226 | free(buffer); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int timestamps_module_redraw(WINDOW *win) |
| 232 | { |
Martin Roth | e81ce04 | 2017-06-03 20:00:36 -0600 | [diff] [blame] | 233 | print_module_title(win, "coreboot Timestamps"); |
Antonello Dettori | 4b1668f | 2016-07-08 11:14:40 +0200 | [diff] [blame] | 234 | |
| 235 | if (!g_buf) |
| 236 | return -1; |
| 237 | |
| 238 | int x = 0, y = 0; |
| 239 | char *tmp = g_buf + g_line * SCREEN_X; |
| 240 | |
| 241 | for (y = 0; y < LINES_SHOWN; y++) { |
| 242 | for (x = 0; x < SCREEN_X; x++) { |
| 243 | mvwaddch(win, y + 2, x, *tmp); |
| 244 | tmp++; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int timestamps_module_handle(int key) |
| 252 | { |
| 253 | if (!g_buf) |
| 254 | return 0; |
| 255 | |
| 256 | switch (key) { |
| 257 | case KEY_DOWN: |
| 258 | g_line++; |
| 259 | break; |
| 260 | case KEY_UP: |
| 261 | g_line--; |
| 262 | break; |
| 263 | case KEY_NPAGE: /* Page up */ |
| 264 | g_line -= LINES_SHOWN; |
| 265 | break; |
| 266 | case KEY_PPAGE: /* Page down */ |
| 267 | g_line += LINES_SHOWN; |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | if (g_line < 0) |
| 272 | g_line = 0; |
| 273 | |
| 274 | if (g_line > g_max_cursor_line) |
| 275 | g_line = g_max_cursor_line; |
| 276 | |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | struct coreinfo_module timestamps_module = { |
| 281 | .name = "Timestamps", |
| 282 | .init = timestamps_module_init, |
| 283 | .redraw = timestamps_module_redraw, |
| 284 | .handle = timestamps_module_handle, |
| 285 | }; |
| 286 | |
| 287 | #else |
| 288 | |
| 289 | struct coreinfo_module timestamps_module = { |
| 290 | }; |
| 291 | |
| 292 | #endif |