forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
241 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#include "trace_service.h" | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <time.h> | ||
#include <stdint.h> | ||
|
||
#define HASH_SIZE 32 | ||
|
||
void | ||
diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec) { | ||
struct timespec end; | ||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end); | ||
int diffsec = end.tv_sec - ti->tv_sec; | ||
assert(diffsec>=0); | ||
int diffnsec = end.tv_nsec - ti->tv_nsec; | ||
if (diffnsec < 0) { | ||
--diffsec; | ||
diffnsec += NANOSEC; | ||
} | ||
*nsec += diffnsec; | ||
if (*nsec > NANOSEC) { | ||
++*sec; | ||
*nsec -= NANOSEC; | ||
} | ||
*sec += diffsec; | ||
} | ||
|
||
struct trace_info { | ||
int session; | ||
struct trace_info * prev; | ||
struct trace_info * next; | ||
struct timespec ti; | ||
uint32_t ti_sec; | ||
uint32_t ti_nsec; | ||
}; | ||
|
||
struct trace_pool { | ||
struct trace_info * current; | ||
struct trace_info * slot[HASH_SIZE]; | ||
}; | ||
|
||
struct trace_pool * | ||
trace_create() { | ||
struct trace_pool * p = malloc(sizeof(*p)); | ||
memset(p, 0, sizeof(*p)); | ||
return p; | ||
} | ||
|
||
static void | ||
_free_slot(struct trace_info *t) { | ||
while (t) { | ||
struct trace_info *next = t->next; | ||
free(t); | ||
t = next; | ||
} | ||
} | ||
|
||
void | ||
trace_release(struct trace_pool *p) { | ||
int i; | ||
for (i=0;i<HASH_SIZE;i++) { | ||
_free_slot(p->slot[i]); | ||
} | ||
free(p->current); | ||
} | ||
|
||
struct trace_info * | ||
trace_new(struct trace_pool *p) { | ||
assert(p->current == NULL); | ||
struct trace_info *t = malloc(sizeof(*t)); | ||
p->current = t; | ||
t->session = 0; | ||
t->prev = NULL; | ||
t->next = NULL; | ||
t->ti_sec = 0; | ||
t->ti_nsec = 0; | ||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t->ti); | ||
return t; | ||
} | ||
|
||
void | ||
trace_register(struct trace_pool *p, int session) { | ||
struct trace_info *t = p->current; | ||
int hash = session % HASH_SIZE; | ||
assert(t->session == 0); | ||
t->session = session; | ||
t->prev = NULL; | ||
t->next = p->slot[hash]; | ||
if (p->slot[hash]) { | ||
p->slot[hash]->prev = t; | ||
} | ||
} | ||
|
||
struct trace_info * | ||
trace_yield(struct trace_pool *p) { | ||
struct trace_info *t = p->current; | ||
if (t == NULL) | ||
return NULL; | ||
p->current = NULL; | ||
|
||
diff_time(&t->ti,&t->ti_sec,&t->ti_nsec); | ||
|
||
if (t->session == 0) { | ||
return t; | ||
} else { | ||
return NULL; | ||
} | ||
} | ||
|
||
void | ||
trace_switch(struct trace_pool *p, int session) { | ||
assert(p->current == NULL); | ||
struct trace_info *t = p->slot[session % HASH_SIZE]; | ||
struct trace_info *prev = NULL; | ||
while (t) { | ||
if (t->session == session) { | ||
p->current = t; | ||
t->session = 0; | ||
if (prev == NULL) { | ||
p->slot[session % HASH_SIZE] = t->next; | ||
} else { | ||
prev->next = t->next; | ||
} | ||
if (t->next) { | ||
t->next->prev = prev; | ||
} | ||
return; | ||
} | ||
prev = t; | ||
t=t->next; | ||
} | ||
} | ||
|
||
double | ||
trace_delete(struct trace_pool *p, struct trace_info *t) { | ||
assert(p->current == t); | ||
p->current = NULL; | ||
if (t) { | ||
double ti = (double)t->ti_sec + (double)t->ti_nsec / NANOSEC; | ||
free(t); | ||
return ti; | ||
} else { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef TRACE_SERVICE_H | ||
#define TRACE_SERVICE_H | ||
|
||
#include <time.h> | ||
#include <stdint.h> | ||
|
||
#define NANOSEC 1000000000 | ||
|
||
void diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec); | ||
|
||
struct trace_pool; | ||
struct trace_info; | ||
|
||
struct trace_pool * trace_create(); | ||
void trace_release(struct trace_pool *); | ||
struct trace_info * trace_new(struct trace_pool *); | ||
void trace_register(struct trace_pool *, int session); | ||
void trace_switch(struct trace_pool *, int session); | ||
struct trace_info * trace_yield(struct trace_pool *); | ||
double trace_delete(struct trace_pool *, struct trace_info *); | ||
|
||
#endif |