Skip to content
Snippets Groups Projects
Commit 2a45a566 authored by Craig Tiller's avatar Craig Tiller
Browse files

Thread local storage for profile data

parent 113d169f
No related branches found
No related tags found
No related merge requests found
...@@ -438,6 +438,9 @@ static grpc_endpoint_write_status grpc_tcp_flush(grpc_tcp *tcp) { ...@@ -438,6 +438,9 @@ static grpc_endpoint_write_status grpc_tcp_flush(grpc_tcp *tcp) {
msg.msg_controllen = 0; msg.msg_controllen = 0;
msg.msg_flags = 0; msg.msg_flags = 0;
GRPC_TIMER_BEGIN(10000, 0);
GRPC_TIMER_END(10000, 0);
GRPC_TIMER_BEGIN(GRPC_PTAG_SENDMSG, 0); GRPC_TIMER_BEGIN(GRPC_PTAG_SENDMSG, 0);
do { do {
/* TODO(klempner): Cork if this is a partial write */ /* TODO(klempner): Cork if this is a partial write */
......
...@@ -53,7 +53,6 @@ typedef enum { ...@@ -53,7 +53,6 @@ typedef enum {
typedef struct grpc_timer_entry { typedef struct grpc_timer_entry {
grpc_precise_clock tm; grpc_precise_clock tm;
gpr_thd_id thd;
int tag; int tag;
marker_type type; marker_type type;
void* id; void* id;
...@@ -61,71 +60,33 @@ typedef struct grpc_timer_entry { ...@@ -61,71 +60,33 @@ typedef struct grpc_timer_entry {
int line; int line;
} grpc_timer_entry; } grpc_timer_entry;
struct grpc_timers_log { #define MAX_COUNT (1024*1024/sizeof(grpc_timer_entry))
gpr_mu mu;
grpc_timer_entry* log;
int num_entries;
int capacity;
int capacity_limit;
FILE* fp;
};
grpc_timers_log* grpc_timers_log_global = NULL; static __thread grpc_timer_entry log[MAX_COUNT];
static __thread int count;
static grpc_timers_log* grpc_timers_log_create(int capacity_limit, FILE* dump) { static void log_report() {
grpc_timers_log* log = gpr_malloc(sizeof(*log));
/* TODO (vpai): Allow allocation below limit */
log->log = gpr_malloc(capacity_limit * sizeof(*log->log));
/* TODO (vpai): Improve concurrency, do per-thread logging? */
gpr_mu_init(&log->mu);
log->num_entries = 0;
log->capacity = log->capacity_limit = capacity_limit;
log->fp = dump;
return log;
}
static void log_report_locked(grpc_timers_log* log) {
FILE* fp = log->fp;
int i; int i;
for (i = 0; i < log->num_entries; i++) { for (i = 0; i < count; i++) {
grpc_timer_entry* entry = &(log->log[i]); grpc_timer_entry* entry = &(log[i]);
fprintf(fp, "GRPC_LAT_PROF "); printf("GRPC_LAT_PROF " GRPC_PRECISE_CLOCK_FORMAT " %p %c %d %p %s %d\n", GRPC_PRECISE_CLOCK_PRINTF_ARGS(&entry->tm), (void*)(gpr_intptr)gpr_thd_currentid(), entry->type, entry->tag,
grpc_precise_clock_print(&entry->tm, fp);
fprintf(fp, " %p %c %d %p %s %d\n", (void*)(gpr_intptr)entry->thd, entry->type, entry->tag,
entry->id, entry->file, entry->line); entry->id, entry->file, entry->line);
} }
/* Now clear out the log */ /* Now clear out the log */
log->num_entries = 0; count = 0;
}
static void grpc_timers_log_destroy(grpc_timers_log* log) {
gpr_mu_lock(&log->mu);
log_report_locked(log);
gpr_mu_unlock(&log->mu);
gpr_free(log->log);
gpr_mu_destroy(&log->mu);
gpr_free(log);
} }
static void grpc_timers_log_add(grpc_timers_log* log, int tag, marker_type type, void* id, static void grpc_timers_log_add(int tag, marker_type type, void* id,
const char* file, int line) { const char* file, int line) {
grpc_timer_entry* entry; grpc_timer_entry* entry;
/* TODO (vpai) : Improve concurrency */ /* TODO (vpai) : Improve concurrency */
gpr_mu_lock(&log->mu); if (count == MAX_COUNT) {
if (log->num_entries == log->capacity_limit) { log_report();
log_report_locked(log);
} }
entry = &log->log[log->num_entries++]; entry = &log[count++];
grpc_precise_clock_now(&entry->tm); grpc_precise_clock_now(&entry->tm);
entry->tag = tag; entry->tag = tag;
...@@ -133,37 +94,32 @@ static void grpc_timers_log_add(grpc_timers_log* log, int tag, marker_type type, ...@@ -133,37 +94,32 @@ static void grpc_timers_log_add(grpc_timers_log* log, int tag, marker_type type,
entry->id = id; entry->id = id;
entry->file = file; entry->file = file;
entry->line = line; entry->line = line;
entry->thd = gpr_thd_currentid();
gpr_mu_unlock(&log->mu);
} }
/* Latency profiler API implementation. */ /* Latency profiler API implementation. */
void grpc_timer_add_mark(int tag, void* id, const char* file, int line) { void grpc_timer_add_mark(int tag, void* id, const char* file, int line) {
if (tag < GRPC_PTAG_IGNORE_THRESHOLD) { if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
grpc_timers_log_add(grpc_timers_log_global, tag, MARK, id, file, line); grpc_timers_log_add(tag, MARK, id, file, line);
} }
} }
void grpc_timer_begin(int tag, void* id, const char *file, int line) { void grpc_timer_begin(int tag, void* id, const char *file, int line) {
if (tag < GRPC_PTAG_IGNORE_THRESHOLD) { if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
grpc_timers_log_add(grpc_timers_log_global, tag, BEGIN, id, file, line); grpc_timers_log_add(tag, BEGIN, id, file, line);
} }
} }
void grpc_timer_end(int tag, void* id, const char *file, int line) { void grpc_timer_end(int tag, void* id, const char *file, int line) {
if (tag < GRPC_PTAG_IGNORE_THRESHOLD) { if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
grpc_timers_log_add(grpc_timers_log_global, tag, END, id, file, line); grpc_timers_log_add(tag, END, id, file, line);
} }
} }
/* Basic profiler specific API functions. */ /* Basic profiler specific API functions. */
void grpc_timers_global_init(void) { void grpc_timers_global_init(void) {
grpc_timers_log_global = grpc_timers_log_create(100000, stdout);
} }
void grpc_timers_global_destroy(void) { void grpc_timers_global_destroy(void) {
grpc_timers_log_destroy(grpc_timers_log_global);
} }
......
...@@ -109,9 +109,7 @@ enum grpc_profiling_tags { ...@@ -109,9 +109,7 @@ enum grpc_profiling_tags {
#endif /* GRPC_STAP_PROFILER */ #endif /* GRPC_STAP_PROFILER */
#ifdef GRPC_BASIC_PROFILER #ifdef GRPC_BASIC_PROFILER
typedef struct grpc_timers_log grpc_timers_log; /* Empty placeholder for now. */
extern grpc_timers_log *grpc_timers_log_global;
#endif /* GRPC_BASIC_PROFILER */ #endif /* GRPC_BASIC_PROFILER */
#endif /* at least one profiler requested. */ #endif /* at least one profiler requested. */
......
...@@ -71,9 +71,8 @@ static double grpc_precise_clock_scaling_factor() { ...@@ -71,9 +71,8 @@ static double grpc_precise_clock_scaling_factor() {
gpr_once_init(&precise_clock_init, grpc_precise_clock_init); gpr_once_init(&precise_clock_init, grpc_precise_clock_init);
return 1e6 / cycles_per_second; return 1e6 / cycles_per_second;
} }
static void grpc_precise_clock_print(const grpc_precise_clock* clk, FILE* fp) { #define GRPC_PRECISE_CLOCK_FORMAT "%f"
fprintf(fp, "%f", *clk * grpc_precise_clock_scaling_factor()); #define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) (*(clk) * grpc_precise_clock_scaling_factor())
}
#else #else
typedef struct grpc_precise_clock grpc_precise_clock; typedef struct grpc_precise_clock grpc_precise_clock;
struct grpc_precise_clock { struct grpc_precise_clock {
...@@ -82,6 +81,8 @@ struct grpc_precise_clock { ...@@ -82,6 +81,8 @@ struct grpc_precise_clock {
static void grpc_precise_clock_now(grpc_precise_clock* clk) { static void grpc_precise_clock_now(grpc_precise_clock* clk) {
clk->clock = gpr_now(); clk->clock = gpr_now();
} }
#define GRPC_PRECISE_CLOCK_FORMAT "%ld.%09d"
#define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) (clk)->clock.tv_sec, (clk)->clock.tv_nsec
static void grpc_precise_clock_print(const grpc_precise_clock* clk, FILE* fp) { static void grpc_precise_clock_print(const grpc_precise_clock* clk, FILE* fp) {
fprintf(fp, "%ld.%09d", clk->clock.tv_sec, clk->clock.tv_nsec); fprintf(fp, "%ld.%09d", clk->clock.tv_sec, clk->clock.tv_nsec);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment