commit 767abfbebf8b3877d3ad8e36ee18ee3bfb826b6e
parent e6c9ce682376b79c92fc9ad9c05f892bdfb6313b
Author: kyle <kyle@0x30.net>
Date: Sat, 19 Nov 2016 21:54:02 -0700
src: trim down runtime
- remove 2 global variables
- improve comments
Diffstat:
4 files changed, 40 insertions(+), 56 deletions(-)
diff --git a/src/rt.c b/src/rt.c
@@ -16,76 +16,71 @@
#include <sys/mman.h> /* mmap */
#include <sys/stat.h> /* S_IRUSR, S_IWUSR, mkdir */
-#include <assert.h>
#include <err.h>
#include <errno.h> /* EEXIST */
#include <fcntl.h> /* O_CREAT */
#include <stdlib.h> /* get{env,progname} */
#include <string.h> /* strlcpy memcpy */
-#include <unistd.h> /* get{cwd,pid,ppid,pgrp} */
+#include <unistd.h> /* lseek get{cwd,pid,ppid,pgrp} */
#include "rt.h" /* struct citrun_{header,node} */
#include "version.h"
-static int init = 0;
static int shm_fd = 0;
-static size_t shm_len = 0;
/*
- * Extends the memory mapping of shm_fd some number of bytes rounded up to the
- * next page size.
- * Exits on error, returns a pointer to the beginning of the extended memory
- * region on success.
+ * Extends the file and memory mapping length of shm_fd by a requested amount of
+ * bytes (rounded up to the next page size).
+ * Returns a pointer to the extended region on success, exits on failure.
*/
static char *
-shm_extend(int bytes)
+shm_extend(size_t requested_bytes)
{
- char *shm;
- int page_size = getpagesize();
- int page_mask = page_size - 1;
- int aligned_bytes;
+ size_t aligned_bytes, page_mask;
+ off_t shm_len;
+ char *shm;
+
+ page_mask = getpagesize() - 1;
+ aligned_bytes = ((requested_bytes) + page_mask) & ~page_mask;
- aligned_bytes = ((bytes) + page_mask) & ~page_mask;
+ /* Get current file length. */
+ if ((shm_len = lseek(shm_fd, 0, SEEK_END)) < 0)
+ err(1, "lseek");
- /* Increase the length of the file descriptor. */
+ /* Increase file length. */
if (ftruncate(shm_fd, shm_len + aligned_bytes) < 0)
- err(1, "ftruncate from %zu to %zu", shm_len, shm_len + aligned_bytes);
+ err(1, "ftruncate from %jd to %jd", shm_len, shm_len + aligned_bytes);
- /* Increase the size of the memory mapping. */
- shm = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED,
+ /* Increase memory mapping length. */
+ shm = mmap(NULL, requested_bytes, PROT_READ | PROT_WRITE, MAP_SHARED,
shm_fd, shm_len);
if (shm == MAP_FAILED)
- err(1, "mmap %i bytes @ %zu", bytes, shm_len);
+ err(1, "mmap %zu bytes @ %jd", requested_bytes, shm_len);
- /* Increase internal length field. */
- shm_len += aligned_bytes;
return shm;
}
/*
- * Add a header region to a newly created shared memory file. Header size is
+ * Add a header region to a newly created shared memory file. Header size is
* rounded up to next page size multiple. Exits on error.
*/
static void
shm_add_header()
{
- char *shm;
+ char *shm;
struct citrun_header header = {
"ctrn",
citrun_major,
- citrun_minor
+ citrun_minor,
+ { getpid(), getppid(), getpgrp() }
};
- header.pids[0] = getpid();
- header.pids[1] = getppid();
- header.pids[2] = getpgrp();
-
- strlcpy(header.progname, getprogname(), CITRUN_PATH_MAX);
+ strlcpy(header.progname, getprogname(), sizeof(header.progname));
- if (getcwd(header.cwd, CITRUN_PATH_MAX) == NULL)
+ if (getcwd(header.cwd, sizeof(header.cwd)) == NULL)
err(1, "getcwd");
shm = shm_extend(sizeof(struct citrun_header));
@@ -104,9 +99,6 @@ shm_create()
char *template = "/tmp/citrun/XXXXXXXXXX";
char *process_dir = "/tmp/citrun";
- assert(shm_fd == 0);
- assert(shm_len == 0);
-
if (getenv("CITRUN_TOOLS") != NULL) {
if ((shm_fd = open("procfile.shm", O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR)) == -1)
@@ -121,18 +113,12 @@ shm_create()
if ((shm_fd = mkstemp(memfile_path)) == -1)
err(1, "mkstemp");
}
-
- init++;
- shm_add_header();
}
/*
- * Public interface, called by instrumented translation units.
- *
- * Copies the passed in citrun_node into the shared memory file.
- * Care is taken to allocate enough memory for the execution buffers which are
- * 8 * L bytes (L = total number of source code lines).
- * Node size is rounded up to the next page size multiple.
+ * Public interface: Called by all instrumented translation units.
+ * Copies n into the shared memory file and then points n->data to a region of
+ * memory located right after n that's at least 8 * n->size large.
* Exits on failure.
*/
void
@@ -147,8 +133,10 @@ citrun_node_add(unsigned int major, unsigned int minor, struct citrun_node *n)
"try cleaning and rebuilding your project",
citrun_major, citrun_minor, major, minor);
- if (!init)
+ if (shm_fd == 0) {
shm_create();
+ shm_add_header();
+ }
sz += sizeof(struct citrun_node);
sz += n->size * sizeof(unsigned long long);
diff --git a/src/rt.h b/src/rt.h
@@ -1,18 +1,16 @@
-#define CITRUN_PATH_MAX 1024
-
struct citrun_header {
char magic[4];
unsigned int major;
unsigned int minor;
unsigned int pids[3];
- char progname[CITRUN_PATH_MAX];
- char cwd[CITRUN_PATH_MAX];
+ char progname[1024];
+ char cwd[1024];
};
struct citrun_node {
unsigned int size;
- const char comp_file_path[CITRUN_PATH_MAX];
- const char abs_file_path[CITRUN_PATH_MAX];
+ const char comp_file_path[1024];
+ const char abs_file_path[1024];
unsigned long long *data;
};
diff --git a/t/inst_preamble.sh b/t/inst_preamble.sh
@@ -12,21 +12,19 @@ cat <<EOF > preamble.c.good
#ifdef __cplusplus
extern "" {
#endif
-#define CITRUN_PATH_MAX 1024
-
struct citrun_header {
char magic[4];
unsigned int major;
unsigned int minor;
unsigned int pids[3];
- char progname[CITRUN_PATH_MAX];
- char cwd[CITRUN_PATH_MAX];
+ char progname[1024];
+ char cwd[1024];
};
struct citrun_node {
unsigned int size;
- const char comp_file_path[CITRUN_PATH_MAX];
- const char abs_file_path[CITRUN_PATH_MAX];
+ const char comp_file_path[1024];
+ const char abs_file_path[1024];
unsigned long long *data;
};
diff --git a/t/utils.subr b/t/utils.subr
@@ -6,7 +6,7 @@ export CITRUN_TOOLS="`pwd`/src"
strip_preamble()
{
file="${1}"
- tail -n +35 $file.citrun > $file.citrun_nohdr
+ tail -n +33 $file.citrun > $file.citrun_nohdr
}
strip_log()