citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 09463b8ec0534855e85fd25a912b469a80dfc21d
parent be4a818a8b0e73771c011d7aa43c2b5d9950e931
Author: Kyle Milz <kyle@0x30.net>
Date:   Thu, 18 Aug 2016 22:43:41 -0600

src: do less copying

Diffstat:
Msrc/runtime.c | 38+++++++++++++++++++++++---------------
Msrc/runtime_proc.cc | 17++++++++++++-----
Msrc/runtime_proc.h | 22+++++++++++-----------
Msrc/shm.cc | 11++++-------
Msrc/shm.h | 7++++++-
5 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/src/runtime.c b/src/runtime.c @@ -33,6 +33,13 @@ static int init = 0; static int shm_fd = 0; static size_t shm_len = 0; +__attribute__((destructor)) +static void clean_up() +{ + if (shm_fd > 0) + (void) shm_unlink(SHM_PATH); +} + size_t add_1(uint8_t *shm, size_t shm_pos, uint8_t data) { @@ -48,13 +55,10 @@ add_4(uint8_t *shm, size_t shm_pos, uint32_t data) } size_t -add_str(uint8_t *shm, size_t shm_pos, const char *data, uint16_t data_sz) +add_str(uint8_t *shm, size_t shm_pos, const char *str, uint16_t null_len) { - memcpy(shm + shm_pos, &data_sz, 2); - shm_pos += 2; - - memcpy(shm + shm_pos, data, data_sz); - return shm_pos + data_sz; + strlcpy(shm + shm_pos, str, null_len); + return shm_pos + null_len; } /* @@ -79,14 +83,12 @@ write_header() if ((cwd_buf = getcwd(NULL, 0)) == NULL) err(1, "getcwd"); - prog_sz = strnlen(progname, PATH_MAX); - cwd_sz = strnlen(cwd_buf, PATH_MAX); + prog_sz = strnlen(progname, PATH_MAX) + 1; + cwd_sz = strnlen(cwd_buf, PATH_MAX) + 1; sz += sizeof(uint8_t) * 2; sz += sizeof(uint32_t) * 3; - sz += sizeof(uint16_t); sz += prog_sz; - sz += sizeof(uint16_t); sz += cwd_sz; if (ftruncate(shm_fd, sz) < 0) @@ -122,7 +124,7 @@ get_shm_fd() if (shm_fd > 0) return shm_fd; - if ((shm_fd = shm_open(SHM_PATH, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) + if ((shm_fd = shm_open(SHM_PATH, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR)) < 0) err(1, "shm_open"); if (init > 0) @@ -156,13 +158,12 @@ citrun_node_add(uint8_t node_major, uint8_t node_minor, struct citrun_node *n) fd = get_shm_fd(); - comp_sz = strnlen(n->comp_file_path, PATH_MAX); - abs_sz = strnlen(n->abs_file_path, PATH_MAX); + comp_sz = strnlen(n->comp_file_path, PATH_MAX) + 1; + abs_sz = strnlen(n->abs_file_path, PATH_MAX) + 1; + sz += sizeof(uint8_t); sz += sizeof(uint32_t); - sz += sizeof(uint16_t); sz += comp_sz; - sz += sizeof(uint16_t); sz += abs_sz; sz += n->size * sizeof(uint64_t); @@ -175,6 +176,10 @@ citrun_node_add(uint8_t node_major, uint8_t node_minor, struct citrun_node *n) err(1, "mmap"); shm_len += sz; + /* Skip past the 'ready' bit location. */ + size_t ready_bit = shm_pos; + shm_pos += 1; + shm_pos = add_4(shm, shm_pos, n->size); shm_pos = add_str(shm, shm_pos, n->comp_file_path, comp_sz); shm_pos = add_str(shm, shm_pos, n->abs_file_path, abs_sz); @@ -183,4 +188,7 @@ citrun_node_add(uint8_t node_major, uint8_t node_minor, struct citrun_node *n) shm_pos += n->size * sizeof(uint64_t); assert(shm_pos == sz); + + /* Flip the ready bit. */ + shm[ready_bit] = 1; } diff --git a/src/runtime_proc.cc b/src/runtime_proc.cc @@ -32,16 +32,22 @@ RuntimeProcess::RuntimeProcess(shm &s) : m_shm.read_all(&m_pid); m_shm.read_all(&m_ppid); m_shm.read_all(&m_pgrp); - m_shm.read_string(m_progname); - m_shm.read_string(m_cwd); + m_shm.read_cstring(&m_progname); + m_shm.read_cstring(&m_cwd); while (m_shm.at_end() == false) { TranslationUnit t; + uint8_t ready = 0; + while (ready == 0) + m_shm.read_pos(&ready); + // Hack to increment the counter. + m_shm.read_all(&ready); + m_shm.read_all(&t.num_lines); - m_shm.read_string(t.comp_file_path); - m_shm.read_string(t.abs_file_path); + m_shm.read_cstring(&t.comp_file_path); + m_shm.read_cstring(&t.abs_file_path); t.exec_diffs = (uint64_t *)m_shm.get_block(t.num_lines * 8); t.source.resize(t.num_lines); @@ -58,7 +64,8 @@ RuntimeProcess::read_source(struct TranslationUnit &t) std::ifstream file_stream(t.abs_file_path); if (file_stream.is_open() == 0) { - warnx("ifstream.open(%s)", t.abs_file_path.c_str()); + warnx("ifstream.open(%s)", t.abs_file_path); + return; } for (auto &l : t.source) diff --git a/src/runtime_proc.h b/src/runtime_proc.h @@ -6,10 +6,10 @@ #include "shm.h" struct TranslationUnit { - std::string comp_file_path; - std::string abs_file_path; - uint32_t num_lines; - uint8_t has_execs; + const char *comp_file_path; + const char *abs_file_path; + uint32_t num_lines; + uint8_t has_execs; uint64_t *exec_diffs; std::vector<std::string> source; }; @@ -19,13 +19,13 @@ public: RuntimeProcess(shm &); void read_executions(); - uint8_t m_major; - uint8_t m_minor; - std::string m_progname; - std::string m_cwd; - uint32_t m_pid; - pid_t m_ppid; - pid_t m_pgrp; + uint8_t m_major; + uint8_t m_minor; + const char *m_progname; + const char *m_cwd; + uint32_t m_pid; + pid_t m_ppid; + pid_t m_pgrp; std::vector<TranslationUnit> m_tus; int m_tus_with_execs; private: diff --git a/src/shm.cc b/src/shm.cc @@ -31,16 +31,13 @@ shm::shm() : } void -shm::read_string(std::string &str) +shm::read_cstring(const char **c_str) { - uint16_t sz; - read_all(&sz); - - if (sz > 1024) + size_t sz = strlen((const char *)m_mem + m_pos) + 1; + if (sz > 1025) errx(1, "read_string: %i too long", sz); - str.resize(sz); - std::copy(m_mem + m_pos, m_mem + m_pos + sz, &str[0]); + *c_str = (const char *)m_mem + m_pos; m_pos += sz; } diff --git a/src/shm.h b/src/shm.h @@ -15,7 +15,12 @@ public: m_pos += sizeof(T); }; - void read_string(std::string &); + void read_pos(uint8_t *bit) + { + *bit = m_mem[m_pos]; + } + + void read_cstring(const char **); void *get_block(size_t); bool at_end();