timing: new minimal timing framework
Before adoption: this commit came in with the history the project adopted, so no landing records it.
To make it easier to add timestamps and timing information for --print-timing-info.
authored by Daniel Eklöf · committed by Daniel Eklöf · parent 82b0558 · Browse files · 6 files·+155 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+155 added−55 removed
- other:+155 added−55 removed
Provenance
- Commit
0221d35- Tree
25dcc01- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
82b0558
6 files changed+155 added−55 removed
modified:main.c+8 added
40 unmodified lines41 #include "xdg.h"42 #include "xmalloc.h"43 #include "xsnprintf.h"44 45+#include "timing.h"46+47 #define max(x, y) ((x) > (y) ? (x) : (y))48 #define min(x, y) ((x) < (y) ? (x) : (y))49 50 struct context {754 unmodified lines805 806 int807 main(int argc, char *const *argv)808 {809+ time_init();810+811 #define OPT_LETTER_SPACING 256812 #define OPT_LAUNCH_PREFIX 257813 #define OPT_SHOW_ACTIONS 258814 #define OPT_MATCH_MODE 2591101 unmodified lines1916 return EXIT_FAILURE;1917 }1918 }1919 1920+1921 log_init(log_colorize, log_syslog, LOG_FACILITY_USER, log_level);1922 LOG_INFO("%s", version_and_features());1923 1924 int ret = EXIT_FAILURE;211 unmodified lines2136 close(STDIN_FILENO); /* To catch reads */2137 }2138 }2139 2140+ if (conf.print_timing_info)2141+ time_enable();2142+2143 _Static_assert((int)LOG_CLASS_ERROR == (int)FCFT_LOG_CLASS_ERROR,2144 "fcft log level enum offset");2145 _Static_assert((int)LOG_COLORIZE_ALWAYS == (int)FCFT_LOG_COLORIZE_ALWAYS,2146 "fcft colorize enum mismatch");213 unmodified linesmodified:meson.build+1 added
211 unmodified lines212 'wayland.c', 'wayland.h',213 'xdg.c', 'xdg.h',214 'xmalloc.c', 'xmalloc.h',215 'xsnprintf.c', 'xsnprintf.h',216+ 'timing.c', 'timing.h',217 wl_proto_src + wl_proto_headers, version, srgb_funcs,218 dependencies: [math,219 threads,220 pixman,56 unmodified linesmodified:render.c+14 added-44 removed
11 unmodified lines12 #include <sys/time.h>13 14 #include "column.h"15 #include "macros.h"16+#include "timing.h"17+18 #if HAS_INCLUDE(<pthread_np.h>)19 #include <pthread_np.h>20 #define pthread_setname_np(thread, name) (pthread_set_name_np(thread, name), 0)21 #elif defined(__NetBSD__)1155 unmodified lines1177 {1178 assert(icon->type == ICON_SVG);1179 1180 if (icon->svg == NULL) {1179- struct timeval start, stop, diff;1180- gettimeofday(&start, NULL);1181+ struct timespec *start_load = time_begin();1182 1182- if (!icon_from_svg(icon, icon->path))1183+ if (!icon_from_svg(icon, icon->path)) {1184+ free(start_load);1185 return;1184-1185- if (print_timing_info) {1186- gettimeofday(&stop, NULL);1187- timersub(&stop, &start, &diff);1188-1189- LOG_WARN("%s loaded in %llus %lluµs",1190- icon->path,1191- (unsigned long long)diff.tv_sec,1192- (unsigned long long)diff.tv_usec);1186 }1187 1188+ time_end(start_load, "%s loaded", icon->path);1189 LOG_DBG("%s", icon->path);1190 }1191 1198- struct timeval start, stop, diff;1199- gettimeofday(&start, NULL);1192+ struct timespec *render_start = time_begin();1193 1194 #if defined(FUZZEL_ENABLE_SVG_LIBRSVG)1195 render_svg_librsvg(icon, x, y, size, cairo);1196 #elif defined(FUZZEL_ENABLE_SVG_NANOSVG)1 unmodified line1198 #elif defined(FUZZEL_ENABLE_SVG_RESVG)1199 render_svg_resvg(icon, x, y, size, pix, cairo, gamma_correct);1200 #endif1201 1209- if (print_timing_info) {1210- gettimeofday(&stop, NULL);1211- timersub(&stop, &start, &diff);1212-1213- LOG_WARN("%s rendered in %llus %lluµs",1214- icon->path,1215- (unsigned long long)diff.tv_sec,1216- (unsigned long long)diff.tv_usec);1217- }1202+ time_end(render_start, "%s rendered", icon->path);1203 }1204 1205 #if defined(FUZZEL_ENABLE_PNG_LIBPNG)1206 static void164 unmodified lines1371 {1372 assert(icon->type == ICON_PNG);1373 1374 if (icon->png == NULL) {1390- struct timeval start, stop, diff;1391- gettimeofday(&start, NULL);1375+ struct timespec *start_load = time_begin();1376 1393- if (!icon_from_png(icon, icon->path, gamma_correct))1377+ if (!icon_from_png(icon, icon->path, gamma_correct)) {1378+ free(start_load);1379 return;1395-1396- if (print_timing_info) {1397- gettimeofday(&stop, NULL);1398- timersub(&stop, &start, &diff);1399- LOG_WARN("%s loaded in %llus %lluµs",1400- icon->path,1401- (unsigned long long)diff.tv_sec,1402- (unsigned long long)diff.tv_usec);1380 }1381 1382+ time_end(start_load, "%s loaded", icon->path);1383 LOG_DBG("%s", icon->path);1384 }1385 1408- struct timeval start, stop, diff;1409- gettimeofday(&start, NULL);1386+ struct timespec *start_render = time_begin();1387 1388 #if defined(FUZZEL_ENABLE_PNG_LIBPNG)1389 render_png_libpng(icon, x, y, size, pix, cairo, scaling_filter);1390 #endif1391 1415- if (print_timing_info) {1416- gettimeofday(&stop, NULL);1417- timersub(&stop, &start, &diff);1418- LOG_WARN("%s rendered in %llus %lluµs",1419- icon->path,1420- (unsigned long long)diff.tv_sec,1421- (unsigned long long)diff.tv_usec);1422- }1392+ time_end(start_render, "%s rendered", icon->path);1393 }1394 1395 static int1396 first_row_y(const struct render *render)582 unmodified linesadded:timing.c+115 added
1+#include "timing.h"2+3+#include <stdarg.h>4+#include <stdio.h>5+#include <stdlib.h>6+7+#define LOG_MODULE "timing"8+#define LOG_ENABLE_DBG 09+#include "log.h"10+#include "xsnprintf.h"11+12+static struct timespec bootup = {0};13+static bool enabled = false;14+15+void16+time_init(void)17+{18+ clock_gettime(CLOCK_MONOTONIC, &bootup);19+}20+21+void22+time_enable(void)23+{24+ enabled = true;25+}26+27+static void28+timespec_sub(const struct timespec *a, const struct timespec *b,29+ struct timespec *res)30+{31+ const long one_sec_in_ns = 1000000000;32+33+ res->tv_sec = a->tv_sec - b->tv_sec;34+ res->tv_nsec = a->tv_nsec - b->tv_nsec;35+36+ /* tv_nsec may be negative */37+ if (res->tv_nsec < 0) {38+ res->tv_sec--;39+ res->tv_nsec += one_sec_in_ns;40+ }41+}42+43+void44+time_since_boot(const char *fmt, ...)45+{46+ if (!enabled)47+ return;48+49+ struct timespec stop;50+ clock_gettime(CLOCK_MONOTONIC, &stop);51+52+ struct timespec diff;53+ timespec_sub(&stop, &bootup, &diff);54+55+ va_list va1, va2;56+ va_start(va1, fmt);57+ va_copy(va2, va1);58+59+ int len = vsnprintf(NULL, 0, fmt, va1);60+ va_end(va1);61+62+ char msg[len + 1];63+ xvsnprintf(msg, len + 1, fmt, va2);64+ va_end(va2);65+66+67+ LOG_WARN("%s: %lds %ldµs since bootup", msg, diff.tv_sec, diff.tv_nsec / 1000);68+}69+70+struct timespec *71+time_begin(void)72+{73+ if (!enabled)74+ return NULL;75+76+ struct timespec *ret = malloc(sizeof(*ret));77+ if (ret == NULL)78+ return NULL;79+80+ clock_gettime(CLOCK_MONOTONIC, ret);81+ return ret;82+}83+84+void85+time_end(struct timespec *start, const char *fmt, ...)86+{87+ if (start == NULL)88+ return;89+90+ if (!enabled) {91+ free(start);92+ return;93+ }94+95+ struct timespec stop;96+ clock_gettime(CLOCK_MONOTONIC, &stop);97+98+ struct timespec diff;99+ timespec_sub(&stop, start, &diff);100+101+ va_list va1, va2;102+ va_start(va1, fmt);103+ va_copy(va2, va1);104+105+ int len = vsnprintf(NULL, 0, fmt, va1);106+ va_end(va1);107+108+ char msg[len + 1];109+ xvsnprintf(msg, len + 1, fmt, va2);110+ va_end(va2);111+112+ LOG_WARN("%s in %lds %ldµs", msg, diff.tv_sec, diff.tv_nsec / 1000);113+114+ free(start);115+}added:timing.h+11 added
1+#pragma once2+3+#include <time.h>4+5+void time_init(void);6+void time_enable(void);7+8+void time_since_boot(const char *fmt, ...) __attribute__((format(printf, 1, 2)));9+10+struct timespec *time_begin(void);11+void time_end(struct timespec *start, const char *fmt, ...) __attribute__((format(printf, 2, 3)));modified:wayland.c+6 added-11 removed
49 unmodified lines50 #include "shm.h"51 #include "xmalloc.h"52 #include "xsnprintf.h"53 54+#include "timing.h"55+56 #define min(x, y) ((x) < (y) ? (x) : (y))57 58 struct font_spec {59 char *pattern;2446 unmodified lines2506 wayl->need_refresh = true;2507 return;2508 }2509 2508- struct timeval start, stop, diff;2509- gettimeofday(&start, NULL);2510+ struct timespec *start_frame = time_begin();2511 2512 if (wayl->chain == NULL) {2513 wayl->chain =2514 shm_chain_new(80 unmodified lines2595 commit:2596 /* No pending frames - render immediately */2597 assert(!wayl->need_refresh);2598 commit_buffer(wayl, buf);2598-2599 done:2600- if (wayl->conf->print_timing_info) {2601- gettimeofday(&stop, NULL);2602- timersub(&stop, &start, &diff);2603- LOG_WARN("%sframe rendered in %llus %lluµs",2604- wayl->render_first_frame_transparent ? "transparent " : "",2605- (unsigned long long)diff.tv_sec,2606- (unsigned long long)diff.tv_usec);2607- }2600+ time_end(2601+ start_frame, "%sframe rendered",2602+ wayl->render_first_frame_transparent ? "transparent " : "");2603 }2604 2605 void2606 wayl_ready_to_display(struct wayland *wayl)551 unmodified lines