profiling: lower overhead when disabled

Instead of a large (6k+) structure in the Packet, make the profiling
storage dynamic. To do this the Packet->profile is now a pointer.

Initial support for selective sampling, e.g. only profile every
1000th packet.
pull/808/merge
Victor Julien 12 years ago
parent c2fcf329f0
commit 13d491f577

@ -526,7 +526,7 @@ typedef struct Packet_
#ifdef PROFILING #ifdef PROFILING
PktProfiling profile; PktProfiling *profile;
#endif #endif
#ifdef __SC_CUDA_SUPPORT__ #ifdef __SC_CUDA_SUPPORT__
CudaPacketVars cuda_pkt_vars; CudaPacketVars cuda_pkt_vars;

@ -560,11 +560,11 @@ void SCProfilingDumpPacketStats(void) {
} }
void SCProfilingPrintPacketProfile(Packet *p) { void SCProfilingPrintPacketProfile(Packet *p) {
if (profiling_packets_csv_enabled == 0 || p == NULL || packet_profile_csv_fp == NULL) { if (profiling_packets_csv_enabled == 0 || p == NULL || packet_profile_csv_fp == NULL || p->profile == NULL) {
return; return;
} }
uint64_t delta = p->profile.ticks_end - p->profile.ticks_start; uint64_t delta = p->profile->ticks_end - p->profile->ticks_start;
fprintf(packet_profile_csv_fp, "%"PRIu64",%c,%"PRIu8",%"PRIu64",", fprintf(packet_profile_csv_fp, "%"PRIu64",%c,%"PRIu8",%"PRIu64",",
p->pcap_cnt, PKT_IS_IPV4(p) ? '4' : (PKT_IS_IPV6(p) ? '6' : '?'), p->proto, p->pcap_cnt, PKT_IS_IPV4(p) ? '4' : (PKT_IS_IPV6(p) ? '6' : '?'), p->proto,
@ -575,7 +575,7 @@ void SCProfilingPrintPacketProfile(Packet *p) {
uint64_t tmm_streamtcp_tcp = 0; uint64_t tmm_streamtcp_tcp = 0;
for (i = 0; i < TMM_SIZE; i++) { for (i = 0; i < TMM_SIZE; i++) {
PktProfilingTmmData *pdt = &p->profile.tmm[i]; PktProfilingTmmData *pdt = &p->profile->tmm[i];
uint64_t tmm_delta = pdt->ticks_end - pdt->ticks_start; uint64_t tmm_delta = pdt->ticks_end - pdt->ticks_start;
fprintf(packet_profile_csv_fp, "%"PRIu64",", tmm_delta); fprintf(packet_profile_csv_fp, "%"PRIu64",", tmm_delta);
@ -590,7 +590,7 @@ void SCProfilingPrintPacketProfile(Packet *p) {
uint64_t app_total = 0; uint64_t app_total = 0;
for (i = 0; i < ALPROTO_MAX; i++) { for (i = 0; i < ALPROTO_MAX; i++) {
PktProfilingAppData *pdt = &p->profile.app[i]; PktProfilingAppData *pdt = &p->profile->app[i];
fprintf(packet_profile_csv_fp,"%"PRIu64",", pdt->ticks_spent); fprintf(packet_profile_csv_fp,"%"PRIu64",", pdt->ticks_spent);
@ -604,10 +604,10 @@ void SCProfilingPrintPacketProfile(Packet *p) {
real_tcp = tmm_streamtcp_tcp - app_total; real_tcp = tmm_streamtcp_tcp - app_total;
fprintf(packet_profile_csv_fp, "%"PRIu64",", real_tcp); fprintf(packet_profile_csv_fp, "%"PRIu64",", real_tcp);
fprintf(packet_profile_csv_fp, "%"PRIu64",", p->profile.proto_detect); fprintf(packet_profile_csv_fp, "%"PRIu64",", p->profile->proto_detect);
for (i = 0; i < PROF_DETECT_SIZE; i++) { for (i = 0; i < PROF_DETECT_SIZE; i++) {
PktProfilingDetectData *pdt = &p->profile.detect[i]; PktProfilingDetectData *pdt = &p->profile->detect[i];
fprintf(packet_profile_csv_fp,"%"PRIu64",", pdt->ticks_spent); fprintf(packet_profile_csv_fp,"%"PRIu64",", pdt->ticks_spent);
} }
@ -639,7 +639,7 @@ static void SCProfilingUpdatePacketDetectRecord(PacketProfileDetectId id, uint8_
void SCProfilingUpdatePacketDetectRecords(Packet *p) { void SCProfilingUpdatePacketDetectRecords(Packet *p) {
PacketProfileDetectId i; PacketProfileDetectId i;
for (i = 0; i < PROF_DETECT_SIZE; i++) { for (i = 0; i < PROF_DETECT_SIZE; i++) {
PktProfilingDetectData *pdt = &p->profile.detect[i]; PktProfilingDetectData *pdt = &p->profile->detect[i];
if (pdt->ticks_spent > 0) { if (pdt->ticks_spent > 0) {
if (PKT_IS_IPV4(p)) { if (PKT_IS_IPV4(p)) {
@ -694,7 +694,7 @@ static void SCProfilingUpdatePacketAppRecord(int alproto, uint8_t ipproto, PktPr
void SCProfilingUpdatePacketAppRecords(Packet *p) { void SCProfilingUpdatePacketAppRecords(Packet *p) {
int i; int i;
for (i = 0; i < ALPROTO_MAX; i++) { for (i = 0; i < ALPROTO_MAX; i++) {
PktProfilingAppData *pdt = &p->profile.app[i]; PktProfilingAppData *pdt = &p->profile->app[i];
if (pdt->ticks_spent > 0) { if (pdt->ticks_spent > 0) {
if (PKT_IS_IPV4(p)) { if (PKT_IS_IPV4(p)) {
@ -705,11 +705,11 @@ void SCProfilingUpdatePacketAppRecords(Packet *p) {
} }
} }
if (p->profile.proto_detect > 0) { if (p->profile->proto_detect > 0) {
if (PKT_IS_IPV4(p)) { if (PKT_IS_IPV4(p)) {
SCProfilingUpdatePacketAppPdRecord(p->proto, p->profile.proto_detect, 4); SCProfilingUpdatePacketAppPdRecord(p->proto, p->profile->proto_detect, 4);
} else { } else {
SCProfilingUpdatePacketAppPdRecord(p->proto, p->profile.proto_detect, 6); SCProfilingUpdatePacketAppPdRecord(p->proto, p->profile->proto_detect, 6);
} }
} }
} }
@ -749,7 +749,7 @@ void SCProfilingUpdatePacketTmmRecord(int module, uint8_t proto, PktProfilingTmm
void SCProfilingUpdatePacketTmmRecords(Packet *p) { void SCProfilingUpdatePacketTmmRecords(Packet *p) {
int i; int i;
for (i = 0; i < TMM_SIZE; i++) { for (i = 0; i < TMM_SIZE; i++) {
PktProfilingTmmData *pdt = &p->profile.tmm[i]; PktProfilingTmmData *pdt = &p->profile->tmm[i];
if (pdt->ticks_start == 0 || pdt->ticks_end == 0 || pdt->ticks_start > pdt->ticks_end) { if (pdt->ticks_start == 0 || pdt->ticks_end == 0 || pdt->ticks_start > pdt->ticks_end) {
continue; continue;
@ -764,7 +764,9 @@ void SCProfilingUpdatePacketTmmRecords(Packet *p) {
} }
void SCProfilingAddPacket(Packet *p) { void SCProfilingAddPacket(Packet *p) {
if (p->profile.ticks_start == 0 || p->profile.ticks_end == 0 || p->profile.ticks_start > p->profile.ticks_end) if (p == NULL || p->profile == NULL ||
p->profile->ticks_start == 0 || p->profile->ticks_end == 0 ||
p->profile->ticks_start > p->profile->ticks_end)
return; return;
pthread_mutex_lock(&packet_profile_lock); pthread_mutex_lock(&packet_profile_lock);
@ -776,7 +778,7 @@ void SCProfilingAddPacket(Packet *p) {
if (PKT_IS_IPV4(p)) { if (PKT_IS_IPV4(p)) {
SCProfilePacketData *pd = &packet_profile_data4[p->proto]; SCProfilePacketData *pd = &packet_profile_data4[p->proto];
uint64_t delta = p->profile.ticks_end - p->profile.ticks_start; uint64_t delta = p->profile->ticks_end - p->profile->ticks_start;
if (pd->min == 0 || delta < pd->min) { if (pd->min == 0 || delta < pd->min) {
pd->min = delta; pd->min = delta;
} }
@ -808,7 +810,7 @@ void SCProfilingAddPacket(Packet *p) {
} else if (PKT_IS_IPV6(p)) { } else if (PKT_IS_IPV6(p)) {
SCProfilePacketData *pd = &packet_profile_data6[p->proto]; SCProfilePacketData *pd = &packet_profile_data6[p->proto];
uint64_t delta = p->profile.ticks_end - p->profile.ticks_start; uint64_t delta = p->profile->ticks_end - p->profile->ticks_start;
if (pd->min == 0 || delta < pd->min) { if (pd->min == 0 || delta < pd->min) {
pd->min = delta; pd->min = delta;
} }
@ -841,6 +843,17 @@ void SCProfilingAddPacket(Packet *p) {
pthread_mutex_unlock(&packet_profile_lock); pthread_mutex_unlock(&packet_profile_lock);
} }
static int rate = 1;
static SC_ATOMIC_DECL_AND_INIT(uint64_t, samples);
PktProfiling *SCProfilePacketStart(void) {
uint64_t sample = SC_ATOMIC_ADD(samples, 1);
if (sample % rate == 0)
return SCCalloc(1, sizeof(PktProfiling));
else
return NULL;
}
#define CASE_CODE(E) case E: return #E #define CASE_CODE(E) case E: return #E
/** /**

@ -85,14 +85,18 @@ extern __thread int profiling_keyword_entered;
profiling_keyword_entered--; \ profiling_keyword_entered--; \
} }
PktProfiling *SCProfilePacketStart(void);
#define PACKET_PROFILING_START(p) \ #define PACKET_PROFILING_START(p) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled) { \
(p)->profile.ticks_start = UtilCpuGetTicks(); \ (p)->profile = SCProfilePacketStart(); \
if ((p)->profile != NULL) \
(p)->profile->ticks_start = UtilCpuGetTicks(); \
} }
#define PACKET_PROFILING_END(p) \ #define PACKET_PROFILING_END(p) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
(p)->profile.ticks_end = UtilCpuGetTicks(); \ (p)->profile->ticks_end = UtilCpuGetTicks(); \
SCProfilingAddPacket((p)); \ SCProfilingAddPacket((p)); \
} }
@ -115,20 +119,20 @@ extern __thread int profiling_keyword_entered;
} while (0) } while (0)
#define PACKET_PROFILING_COPY_LOCKS(p, id) do { \ #define PACKET_PROFILING_COPY_LOCKS(p, id) do { \
(p)->profile.tmm[(id)].mutex_lock_cnt = mutex_lock_cnt; \ (p)->profile->tmm[(id)].mutex_lock_cnt = mutex_lock_cnt; \
(p)->profile.tmm[(id)].mutex_lock_wait_ticks = mutex_lock_wait_ticks; \ (p)->profile->tmm[(id)].mutex_lock_wait_ticks = mutex_lock_wait_ticks; \
(p)->profile.tmm[(id)].mutex_lock_contention = mutex_lock_contention; \ (p)->profile->tmm[(id)].mutex_lock_contention = mutex_lock_contention; \
(p)->profile.tmm[(id)].spin_lock_cnt = spin_lock_cnt; \ (p)->profile->tmm[(id)].spin_lock_cnt = spin_lock_cnt; \
(p)->profile.tmm[(id)].spin_lock_wait_ticks = spin_lock_wait_ticks; \ (p)->profile->tmm[(id)].spin_lock_wait_ticks = spin_lock_wait_ticks; \
(p)->profile.tmm[(id)].spin_lock_contention = spin_lock_contention; \ (p)->profile->tmm[(id)].spin_lock_contention = spin_lock_contention; \
(p)->profile.tmm[(id)].rww_lock_cnt = rww_lock_cnt; \ (p)->profile->tmm[(id)].rww_lock_cnt = rww_lock_cnt; \
(p)->profile.tmm[(id)].rww_lock_wait_ticks = rww_lock_wait_ticks; \ (p)->profile->tmm[(id)].rww_lock_wait_ticks = rww_lock_wait_ticks; \
(p)->profile.tmm[(id)].rww_lock_contention = rww_lock_contention; \ (p)->profile->tmm[(id)].rww_lock_contention = rww_lock_contention; \
(p)->profile.tmm[(id)].rwr_lock_cnt = rwr_lock_cnt; \ (p)->profile->tmm[(id)].rwr_lock_cnt = rwr_lock_cnt; \
(p)->profile.tmm[(id)].rwr_lock_wait_ticks = rwr_lock_wait_ticks; \ (p)->profile->tmm[(id)].rwr_lock_wait_ticks = rwr_lock_wait_ticks; \
(p)->profile.tmm[(id)].rwr_lock_contention = rwr_lock_contention; \ (p)->profile->tmm[(id)].rwr_lock_contention = rwr_lock_contention; \
record_locks = 0;\ record_locks = 0; \
SCProfilingAddPacketLocks((p)); \ SCProfilingAddPacketLocks((p)); \
} while(0) } while(0)
#else #else
#define PACKET_PROFILING_RESET_LOCKS #define PACKET_PROFILING_RESET_LOCKS
@ -136,24 +140,25 @@ extern __thread int profiling_keyword_entered;
#endif #endif
#define PACKET_PROFILING_TMM_START(p, id) \ #define PACKET_PROFILING_TMM_START(p, id) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < TMM_SIZE) { \ if ((id) < TMM_SIZE) { \
(p)->profile.tmm[(id)].ticks_start = UtilCpuGetTicks(); \ (p)->profile->tmm[(id)].ticks_start = UtilCpuGetTicks();\
PACKET_PROFILING_RESET_LOCKS; \ PACKET_PROFILING_RESET_LOCKS; \
} \ } \
} }
#define PACKET_PROFILING_TMM_END(p, id) \ #define PACKET_PROFILING_TMM_END(p, id) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < TMM_SIZE) { \ if ((id) < TMM_SIZE) { \
PACKET_PROFILING_COPY_LOCKS((p), (id)); \ PACKET_PROFILING_COPY_LOCKS((p), (id)); \
(p)->profile.tmm[(id)].ticks_end = UtilCpuGetTicks(); \ (p)->profile->tmm[(id)].ticks_end = UtilCpuGetTicks(); \
} \ } \
} }
#define PACKET_PROFILING_RESET(p) \ #define PACKET_PROFILING_RESET(p) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
memset(&(p)->profile, 0x00, sizeof(PktProfiling)); \ SCFree((p)->profile); \
(p)->profile = NULL; \
} }
#define PACKET_PROFILING_APP_START(dp, id) \ #define PACKET_PROFILING_APP_START(dp, id) \
@ -197,28 +202,28 @@ extern __thread int profiling_keyword_entered;
} }
#define PACKET_PROFILING_APP_STORE(dp, p) \ #define PACKET_PROFILING_APP_STORE(dp, p) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((dp)->alproto < ALPROTO_MAX) { \ if ((dp)->alproto < ALPROTO_MAX) { \
(p)->profile.app[(dp)->alproto].ticks_spent += (dp)->ticks_spent; \ (p)->profile->app[(dp)->alproto].ticks_spent += (dp)->ticks_spent; \
(p)->profile.proto_detect += (dp)->proto_detect_ticks_spent; \ (p)->profile->proto_detect += (dp)->proto_detect_ticks_spent; \
} \ } \
} }
#define PACKET_PROFILING_DETECT_START(p, id) \ #define PACKET_PROFILING_DETECT_START(p, id) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < PROF_DETECT_SIZE) { \ if ((id) < PROF_DETECT_SIZE) { \
(p)->profile.detect[(id)].ticks_start = UtilCpuGetTicks(); \ (p)->profile->detect[(id)].ticks_start = UtilCpuGetTicks(); \
} \ } \
} }
#define PACKET_PROFILING_DETECT_END(p, id) \ #define PACKET_PROFILING_DETECT_END(p, id) \
if (profiling_packets_enabled) { \ if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < PROF_DETECT_SIZE) { \ if ((id) < PROF_DETECT_SIZE) { \
(p)->profile.detect[(id)].ticks_end = UtilCpuGetTicks();\ (p)->profile->detect[(id)].ticks_end = UtilCpuGetTicks();\
if ((p)->profile.detect[(id)].ticks_start != 0 && \ if ((p)->profile->detect[(id)].ticks_start != 0 && \
(p)->profile.detect[(id)].ticks_start < (p)->profile.detect[(id)].ticks_end) { \ (p)->profile->detect[(id)].ticks_start < (p)->profile->detect[(id)].ticks_end) { \
(p)->profile.detect[(id)].ticks_spent += \ (p)->profile->detect[(id)].ticks_spent += \
((p)->profile.detect[(id)].ticks_end - (p)->profile.detect[(id)].ticks_start); \ ((p)->profile->detect[(id)].ticks_end - (p)->profile->detect[(id)].ticks_start); \
} \ } \
} \ } \
} }

Loading…
Cancel
Save