| 1 | #cxx . . $ `<file>/src/include/samplefile.hpp`/pragma once |
| 2 | #ifndef SAMPLEFILE_H |
| 3 | #define SAMPLEFILE_Hcxx . . $ `src/include/samplefile.hpp:3:9`! |
| 4 | |
| 5 | #include <errno.h> |
| 6 | #include <heaplayers.h>cxx . . $ `<file>/vendor/Heap-Layers/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"cxx . . $ `<file>/vendor/printf/printf.h`/ |
| 20 | |
| 21 | // Handles creation, deletion, and concurrency control |
| 22 | // signal files in memory |
| 23 | |
| 24 | class SampleFilecxx . . $ SampleFile# { |
| 25 | public: |
| 26 | static constexpr int MAX_BUFSIZEcxx . . $ SampleFile#MAX_BUFSIZE. = |
| 27 | 4096; // actual (and maximum) length of a line passed to writeToFile |
| 28 | private: |
| 29 | static constexpr int LOCK_FD_SIZEcxx . . $ SampleFile#LOCK_FD_SIZE. = 4096; |
| 30 | static constexpr int MAX_FILE_SIZEcxx . . $ SampleFile#MAX_FILE_SIZE. = 4096 * 65536; |
| 31 | |
| 32 | static char *initializercxx . . $ SampleFile#initializer.; |
| 33 | |
| 34 | public: |
| 35 | SampleFilecxx . . $ SampleFile#SampleFile(facfdab5246d4be7).(const char *filename_template, const char *lockfilename_template, |
| 36 | const char *init_template) { |
| 37 | static uint base_pid = getpid(); |
| 38 | snprintfcxx . . $ `vendor/printf/printf.h:85:9`!(_init_filenamecxx . . $ SampleFile#_init_filename., PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! - 1, init_template, base_pid); |
| 39 | snprintfcxx . . $ `vendor/printf/printf.h:85:9`!(_signalfilecxx . . $ SampleFile#_signalfile., PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! - 1, filename_template, base_pid); |
| 40 | snprintfcxx . . $ `vendor/printf/printf.h:85:9`!(_lockfilecxx . . $ SampleFile#_lockfile., PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! - 1, lockfilename_template, base_pid); |
| 41 | int signal_fd = open(_signalfilecxx . . $ SampleFile#_signalfile., flagscxx . . $ SampleFile#flags., permscxx . . $ SampleFile#perms.); |
| 42 | int lock_fd = open(_lockfilecxx . . $ SampleFile#_lockfile., flagscxx . . $ SampleFile#flags., permscxx . . $ SampleFile#perms.); |
| 43 | if ((signal_fd == -1) || (lock_fd == -1)) { |
| 44 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, __FILE__, |
| 45 | __LINE__); |
| 46 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 47 | } |
| 48 | if (ftruncate(signal_fd, MAX_FILE_SIZEcxx . . $ SampleFile#MAX_FILE_SIZE.) != 0) { |
| 49 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, __FILE__, |
| 50 | __LINE__); |
| 51 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 52 | } |
| 53 | if (ftruncate(lock_fd, LOCK_FD_SIZEcxx . . $ SampleFile#LOCK_FD_SIZE.) != 0) { |
| 54 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, __FILE__, |
| 55 | __LINE__); |
| 56 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 57 | } |
| 58 | _mmapcxx . . $ SampleFile#_mmap. = reinterpret_cast<char *>(mmap( |
| 59 | 0, MAX_FILE_SIZEcxx . . $ SampleFile#MAX_FILE_SIZE., PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!, MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!, signal_fd, 0)); |
| 60 | _lastposcxx . . $ SampleFile#_lastpos. = reinterpret_cast<uint64_tcxx . . $ uint64_t# *>( |
| 61 | mmap(0, LOCK_FD_SIZEcxx . . $ SampleFile#LOCK_FD_SIZE., PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!, MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!, 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 (_mmapcxx . . $ SampleFile#_mmap. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) { |
| 67 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, __FILE__, |
| 68 | __LINE__); |
| 69 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 70 | } |
| 71 | if (_lastposcxx . . $ SampleFile#_lastpos. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) { |
| 72 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, __FILE__, |
| 73 | __LINE__); |
| 74 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 75 | } |
| 76 | // This is a miserable hack that does not deserve to exist |
| 77 | int init_fd = open(_init_filenamecxx . . $ SampleFile#_init_filename., O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`! | O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!, permscxx . . $ SampleFile#perms.); |
| 78 | int res = flock(init_fd, LOCK_EXcxx . . $ `<external>/8566841665215861632/file.h:41:9`!); |
| 79 | if (res) { |
| 80 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, __FILE__, |
| 81 | __LINE__); |
| 82 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 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_lockcxx . . $ SampleFile#_spin_lock. = (HLcxx . . $ HL/::SpinLockcxx . . $ HL/SpinLock# *)(((char *)_lastposcxx . . $ SampleFile#_lastpos.) + sizeof(uint64_tcxx . . $ uint64_t#)); |
| 103 | } else { |
| 104 | // tprintf::tprintf("@ SampleFile::SampleFile initializing\n", |
| 105 | // (void*)_mmap); |
| 106 | if (write(init_fd, "q&", 3) != 3) { |
| 107 | fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Scalene: internal error = %d (%s:%d)\n", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, |
| 108 | __FILE__, __LINE__); |
| 109 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 110 | } |
| 111 | fsync(init_fd); |
| 112 | _spin_lockcxx . . $ SampleFile#_spin_lock. = new (((char *)_lastposcxx . . $ SampleFile#_lastpos.) + sizeof(uint64_tcxx . . $ uint64_t#)) HLcxx . . $ HL/::SpinLockcxx . . $ HL/SpinLock#(); |
| 113 | *_lastposcxx . . $ SampleFile#_lastpos. = 0; |
| 114 | } |
| 115 | |
| 116 | flock(init_fd, LOCK_UNcxx . . $ `<external>/8566841665215861632/file.h:42:9`!); |
| 117 | close(init_fd); |
| 118 | } |
| 119 | ~cxx . . $ SampleFile#`~SampleFile`(49f6e7a06ebc5aa8).SampleFilecxx . . $ 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(_signalfilecxx . . $ SampleFile#_signalfile.); |
| 128 | unlink(_lockfilecxx . . $ SampleFile#_lockfile.); |
| 129 | unlink(_init_filenamecxx . . $ SampleFile#_init_filename.); |
| 130 | } |
| 131 | void writeToFilecxx . . $ SampleFile#writeToFile(fda57c8427c60377).(char *line) { // , int is_malloc) { |
| 132 | _spin_lockcxx . . $ SampleFile#_spin_lock.->lockcxx . . $ HL/SpinLockType#lock(49f6e7a06ebc5aa8).(); |
| 133 | strncpy(_mmapcxx . . $ SampleFile#_mmap. + *_lastposcxx . . $ SampleFile#_lastpos., (const char *)line, MAX_BUFSIZEcxx . . $ SampleFile#MAX_BUFSIZE.); |
| 134 | |
| 135 | *_lastposcxx . . $ SampleFile#_lastpos. += strlen(_mmapcxx . . $ SampleFile#_mmap. + *_lastposcxx . . $ SampleFile#_lastpos.) - 1; |
| 136 | // tprintf::tprintf("@ wrote @, lastpos=@\n", (void*)_mmap, line, |
| 137 | // *_lastpos); |
| 138 | _spin_lockcxx . . $ SampleFile#_spin_lock.->unlockcxx . . $ HL/SpinLockType#unlock(49f6e7a06ebc5aa8).(); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | // Prevent copying and assignment. |
| 143 | SampleFilecxx . . $ SampleFile#SampleFile(b45aaee773adbdd3).(const SampleFilecxx . . $ SampleFile# &) = delete; |
| 144 | SampleFilecxx . . $ SampleFile# &operatorcxx . . $ SampleFile#`operator=`(d241a7135c93f10f).=(const SampleFilecxx . . $ SampleFile# &) = delete; |
| 145 | |
| 146 | // Flags for the mmap regions |
| 147 | static constexpr auto flagscxx . . $ SampleFile#flags. = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!; |
| 148 | static constexpr auto permscxx . . $ SampleFile#perms. = S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`! | S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!; |
| 149 | |
| 150 | char |
| 151 | _signalfilecxx . . $ SampleFile#_signalfile.[MAX_BUFSIZEcxx . . $ SampleFile#MAX_BUFSIZE.]; // Name of log file that signals are written to |
| 152 | char _lockfilecxx . . $ SampleFile#_lockfile.[MAX_BUFSIZEcxx . . $ SampleFile#MAX_BUFSIZE.]; // Name of file that _lastpos is persisted in |
| 153 | char _init_filenamecxx . . $ SampleFile#_init_filename.[MAX_BUFSIZEcxx . . $ SampleFile#MAX_BUFSIZE.]; // initializer filename |
| 154 | char *_mmapcxx . . $ SampleFile#_mmap.; // address of first byte of log |
| 155 | uint64_tcxx . . $ uint64_t# *_lastposcxx . . $ SampleFile#_lastpos.; // address of first byte of _lastpos |
| 156 | HLcxx . . $ HL/::SpinLockcxx . . $ HL/SpinLock# *_spin_lockcxx . . $ SampleFile#_spin_lock.; |
| 157 | }; |
| 158 | |
| 159 | #endif |