Debian Codegraph @

src/include/samplefile.hpp

raw symbols SCIP
1 #pragma once
2 #ifndef SAMPLEFILE_H
3 #define SAMPLEFILE_H
4
5 #include <errno.h>
6 #include <heaplayers.h>
7
8 #if !defined(_WIN32)
9 #include <pthread.h>
10 #include <sys/file.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 #else
14 #include <sys/stat.h>
15 #define S_IRUSR _S_IRUSR
16 #define S_IWUSR _S_IWUSR
17 #endif
18
19 #include "printf.h"
20
21 // Handles creation, deletion, and concurrency control
22 // signal files in memory
23
24 class SampleFile {
25 public:
26 static constexpr int MAX_BUFSIZE =
27 4096; // actual (and maximum) length of a line passed to writeToFile
28 private:
29 static constexpr int LOCK_FD_SIZE = 4096;
30 static constexpr int MAX_FILE_SIZE = 4096 * 65536;
31
32 static char *initializer;
33
34 public:
35 SampleFile(const char *filename_template, const char *lockfilename_template,
36 const char *init_template) {
37 static uint base_pid = getpid();
38 snprintf(_init_filename, PATH_MAX - 1, init_template, base_pid);
39 snprintf(_signalfile, PATH_MAX - 1, filename_template, base_pid);
40 snprintf(_lockfile, PATH_MAX - 1, lockfilename_template, base_pid);
41 int signal_fd = open(_signalfile, flags, perms);
42 int lock_fd = open(_lockfile, flags, perms);
43 if ((signal_fd == -1) || (lock_fd == -1)) {
44 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno, __FILE__,
45 __LINE__);
46 abort();
47 }
48 if (ftruncate(signal_fd, MAX_FILE_SIZE) != 0) {
49 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno, __FILE__,
50 __LINE__);
51 abort();
52 }
53 if (ftruncate(lock_fd, LOCK_FD_SIZE) != 0) {
54 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno, __FILE__,
55 __LINE__);
56 abort();
57 }
58 _mmap = reinterpret_cast<char *>(mmap(
59 0, MAX_FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, signal_fd, 0));
60 _lastpos = reinterpret_cast<uint64_t *>(
61 mmap(0, LOCK_FD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, lock_fd, 0));
62 // tprintf::tprintf("@ SampleFile::SampleFile @ _lastpos=@\n", (void*)_mmap,
63 // _signalfile, *_lastpos);
64 close(signal_fd);
65 close(lock_fd);
66 if (_mmap == MAP_FAILED) {
67 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno, __FILE__,
68 __LINE__);
69 abort();
70 }
71 if (_lastpos == MAP_FAILED) {
72 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno, __FILE__,
73 __LINE__);
74 abort();
75 }
76 // This is a miserable hack that does not deserve to exist
77 int init_fd = open(_init_filename, O_CREAT | O_RDWR, perms);
78 int res = flock(init_fd, LOCK_EX);
79 if (res) {
80 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno, __FILE__,
81 __LINE__);
82 abort();
83 }
84 char buf[4];
85 memset(buf, 0, 4);
86 // A samplefile may be initialized
87 // multiple times from the same template,
88 // so access must be synchronized.
89 // the corresponding file/memory regions
90 // have been initialized,
91 // the lockfile will have the string "q&"
92 // at the beginning. Otherwise, it is written after initialization
93 //
94 // 3 bytes are read to bring in both the magic string and the end-of-string
95 // character "q&\0"
96 // fsync(init_fd);
97
98 int amt_read = read(init_fd, buf, 3);
99 if (amt_read != 0 && strcmp(buf, "q&") == 0) {
100 // If magic number is present, we know that a HL::SpinLock has already
101 // been initialized
102 _spin_lock = (HL::SpinLock *)(((char *)_lastpos) + sizeof(uint64_t));
103 } else {
104 // tprintf::tprintf("@ SampleFile::SampleFile initializing\n",
105 // (void*)_mmap);
106 if (write(init_fd, "q&", 3) != 3) {
107 fprintf(stderr, "Scalene: internal error = %d (%s:%d)\n", errno,
108 __FILE__, __LINE__);
109 abort();
110 }
111 fsync(init_fd);
112 _spin_lock = new (((char *)_lastpos) + sizeof(uint64_t)) HL::SpinLock();
113 *_lastpos = 0;
114 }
115
116 flock(init_fd, LOCK_UN);
117 close(init_fd);
118 }
119 ~SampleFile() {
120 // NOTE: These unmaps were causing issues
121 // sometimes leading to a SIGSEGV when BLAS
122 // threads were tearing down.
123 // Removing these unmaps resolved the issue
124 //
125 // munmap(_mmap, MAX_FILE_SIZE);
126 // munmap(_lastpos, LOCK_FD_SIZE);
127 unlink(_signalfile);
128 unlink(_lockfile);
129 unlink(_init_filename);
130 }
131 void writeToFile(char *line) { // , int is_malloc) {
132 _spin_lock->lock();
133 strncpy(_mmap + *_lastpos, (const char *)line, MAX_BUFSIZE);
134
135 *_lastpos += strlen(_mmap + *_lastpos) - 1;
136 // tprintf::tprintf("@ wrote @, lastpos=@\n", (void*)_mmap, line,
137 // *_lastpos);
138 _spin_lock->unlock();
139 }
140
141 private:
142 // Prevent copying and assignment.
143 SampleFile(const SampleFile &) = delete;
144 SampleFile &operator=(const SampleFile &) = delete;
145
146 // Flags for the mmap regions
147 static constexpr auto flags = O_RDWR | O_CREAT;
148 static constexpr auto perms = S_IRUSR | S_IWUSR;
149
150 char
151 _signalfile[MAX_BUFSIZE]; // Name of log file that signals are written to
152 char _lockfile[MAX_BUFSIZE]; // Name of file that _lastpos is persisted in
153 char _init_filename[MAX_BUFSIZE]; // initializer filename
154 char *_mmap; // address of first byte of log
155 uint64_t *_lastpos; // address of first byte of _lastpos
156 HL::SpinLock *_spin_lock;
157 };
158
159 #endif