Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created February 9, 2019 11:11
Show Gist options
  • Save ilyaevseev/9aa5e264a9763621fab8598c81092599 to your computer and use it in GitHub Desktop.
Save ilyaevseev/9aa5e264a9763621fab8598c81092599 to your computer and use it in GitHub Desktop.
Inspired by https://forum.nginx.org/read.php?21,282919 -- print CPU ticks to Nginx debug log
diff -r 992bf7540a98 src/core/ngx_log.c
--- a/src/core/ngx_log.c Thu Feb 07 19:39:35 2019 +0300
+++ b/src/core/ngx_log.c Sat Feb 09 13:14:06 2019 +0300
@@ -16,6 +16,8 @@ static void ngx_log_insert(ngx_log_t *lo
#if (NGX_DEBUG)
+static u_char *ngx_log_cputicks(u_char *p, u_char *last);
+
static void ngx_log_memory_writer(ngx_log_t *log, ngx_uint_t level,
u_char *buf, size_t len);
static void ngx_log_memory_cleanup(void *data);
@@ -145,6 +147,14 @@ ngx_log_error_core(ngx_uint_t level, ngx
p = ngx_log_errno(p, last, err);
}
+#if (NGX_DEBUG)
+
+ if (level >= NGX_LOG_DEBUG) {
+ p = ngx_log_cputicks(p, last);
+ }
+
+#endif
+
if (level != NGX_LOG_DEBUG && log->handler) {
p = log->handler(log, p, last - p);
}
@@ -712,6 +722,17 @@ ngx_log_insert(ngx_log_t *log, ngx_log_t
#if (NGX_DEBUG)
+static u_char*
+ngx_log_cputicks(u_char *p, u_char *last)
+{
+ uint64_t lo = 0, hi = 0, ticks = 0;
+
+ __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
+ ticks = ((u_int64_t)hi << 32) | lo;
+
+ return ngx_sprintf(p, " cputicks=%uL", ticks);
+}
+
static void
ngx_log_memory_writer(ngx_log_t *log, ngx_uint_t level, u_char *buf,
size_t len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment