Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/logging/lib/load-log.cxx
ViewVC logotype

Annotation of /branches/vis15/src/logging/lib/load-log.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4889 - (view) (download) (as text)

1 : jhr 4848 /*! \file load-log.cxx
2 :     *
3 :     * \author John Reppy
4 :     */
5 :    
6 :     /*
7 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     *
9 :     * COPYRIGHT (c) 2016 The University of Chicago
10 :     * All rights reserved.
11 :     */
12 :    
13 :     #include "diderot/log-file.hxx"
14 :     #include "load-log.hxx"
15 :     #include <algorithm>
16 :     #include <cstdlib>
17 :     #include <fstream>
18 :     #include <iostream>
19 : jhr 4879 #include <iomanip>
20 : jhr 4848 #include <sys/stat.h>
21 :    
22 :     // comparison fucntion for events. Returns true if ev1 < ev2 when ordered
23 :     // first by time stamps and then by worker ID.
24 :     //
25 :     static bool compare_events (event const &ev1, event const &ev2)
26 :     {
27 :     return (ev1._ts < ev2._ts) || ((ev1._ts == ev2._ts) && (ev1._worker < ev2._worker));
28 :     }
29 :    
30 :     // convert a timestamp to nanoseconds
31 :     //
32 : jhr 4879 inline uint64_t get_timestamp (int kind, const diderot::log::time_stamp *ts)
33 : jhr 4848 {
34 :     if (kind == diderot::log::TS_MACH_ABSOLUTE)
35 :     return ts->_mach;
36 :     else if (kind == diderot::log::TS_TIMESPEC)
37 : jhr 4882 return static_cast<int64_t>(ts->_tv._sec) * 1000000000 + static_cast<int64_t>(ts->_tv._frac);
38 : jhr 4848 else /* kind == diderot::log::TS_TIMEVAL */
39 : jhr 4882 return static_cast<int64_t>(ts->_tv._sec) * 1000000000 + static_cast<int64_t>(ts->_tv._frac) * 1000;
40 : jhr 4848 }
41 :    
42 : jhr 4879 static void dump_buffer (diderot::log::file_hdr const &hdr, diderot::log::buffer const &buf)
43 :     {
44 :     auto wid = std::cerr.width();
45 :     std::cerr << "##### BUFFER: worker = " << buf._worker << "; seq# = " << buf._seqNum << "\n";
46 :     for (uint32_t ix = 0; ix < buf._next; ix++) {
47 : jhr 4883 const diderot::log::event *ep = &buf._log[ix];
48 : jhr 4879 std::cerr << std::setw(3) << ix << ":";
49 : jhr 4881 const uint32_t *ip = reinterpret_cast<const uint32_t *>(ep);
50 : jhr 4882 std::cerr << std::setfill('0') << std::hex;
51 : jhr 4881 for (uint32_t j = 0; j < sizeof(event)/4; j++) {
52 :     std::cerr << " " << std::setw(8) << ip[j];
53 : jhr 4879 }
54 : jhr 4880 std::cerr << std::setfill(' ') << std::dec << " { ";
55 : jhr 4879 uint64_t ts = get_timestamp(hdr._tsKind, &ep->_ts);
56 :     std::cerr << "[" << (ts / 1000000000) << "."
57 :     << std::setfill('0') << std::setw(9) << (ts % 1000000000)
58 :     << std::setfill(' ') << std::setw(wid)
59 :     << "] " << ep->_strand << " " << ep->_event << "}\n";
60 :    
61 :     }
62 :     std::cerr << "#####\n";
63 :     }
64 :    
65 : jhr 4848 // load a log file
66 :     //
67 : jhr 4872 log_file *load_log_file (std::string const &name, bool noSort)
68 : jhr 4848 {
69 :     // get the file size and compute the expected number
70 :     // of buffers.
71 :     int numBufs;
72 :     {
73 :     struct stat st;
74 :     if (stat(name.c_str(), &st) < 0) {
75 :     perror ("stat");
76 :     exit (1);
77 :     }
78 : jhr 4855 numBufs = (st.st_size - sizeof(diderot::log::file_hdr)) / sizeof(diderot::log::buffer);
79 :     if (st.st_size != sizeof(diderot::log::file_hdr) + numBufs * sizeof(diderot::log::buffer)) {
80 : jhr 4848 std::cerr << "Warning: log file size is not a whole number of buffers\n";
81 :     }
82 :     }
83 :    
84 :     // open the file
85 :     std::ifstream ins(name, std::ifstream::in | std::ifstream::binary);
86 : jhr 4855 if (ins.fail()) {
87 : jhr 4848 std::cerr << "Error: unable to open \"" << name << "\"\n";
88 :     exit (1);
89 :     }
90 :    
91 :     // read the header
92 :     diderot::log::file_hdr hdr;
93 :     ins.read (reinterpret_cast<char *>(&hdr), sizeof(hdr));
94 : jhr 4855 if (ins.fail()) {
95 : jhr 4848 std::cerr << "Error attempting to read header from \"" << name << "\"\n";
96 :     exit (1);
97 :     }
98 :    
99 :     // check the header
100 :     if (hdr._magic != diderot::log::file_hdr::MAGIC) {
101 :     std::cerr << "bogus magic number\n";
102 :     exit (1);
103 :     }
104 :     if (hdr._version[0] != DIDEROT_LOG_VERSION_MAJOR) {
105 :     std::cerr << "wrong version = " << hdr._version[0] << "." << hdr._version[1]
106 :     << "." << hdr._version[2] << " (expected " << DIDEROT_LOG_VERSION_MAJOR << ".x.y)\n";
107 :     exit (1);
108 :     }
109 :     if (hdr._version[1] != 0) {
110 :     std::cerr << "fat-event format not supported\n";
111 :     exit (1);
112 :     }
113 : jhr 4874 if (hdr._hdrSzB != sizeof(diderot::log::file_hdr)) {
114 :     std::cerr << "bogus header size " << hdr._hdrSzB << " (expected "
115 :     << sizeof(diderot::log::file_hdr) << ")\n";
116 :     exit (1);
117 :     }
118 : jhr 4876 if (hdr._eventSzB != sizeof(diderot::log::event)) {
119 :     std::cerr << "using different event size " << hdr._eventSzB << " (expected "
120 : jhr 4874 << sizeof(diderot::log::event) << ")\n";
121 :     exit (1);
122 :     }
123 : jhr 4848 if (hdr._bufSzB != diderot::log::buffer::SIZEB) {
124 :     std::cerr << "using different block size " << hdr._bufSzB << " (expected "
125 :     << diderot::log::buffer::SIZEB << ")\n";
126 :     exit (1);
127 :     }
128 :    
129 : jhr 4874 // get the start time
130 : jhr 4876 uint64_t startTime = get_timestamp(hdr._tsKind, &hdr._startTime);
131 : jhr 4874
132 : jhr 4848 // compute an upper bound on the number of events in the file
133 :     uint64_t maxNumEvents = diderot::log::buffer::NUM_EVENTS * numBufs;
134 :    
135 :     // initialize the in-memory log file
136 :     log_file *log = new log_file;
137 : jhr 4889 log->_date = std::string(hdr._date);
138 :     log->_clockName = std::string(hdr._clockName);
139 : jhr 4878 log->_startTime = startTime;
140 :     log->_resolution = hdr._resolution;
141 : jhr 4848 log->_nNodes = hdr._nNodes;
142 :     log->_nCores = hdr._nCores;
143 :     log->_nWorkers = hdr._nWorkers;
144 :     log->_events.reserve (maxNumEvents);
145 :    
146 : jhr 4868 // track last timestamp per worker to detect anomolies
147 :     uint64_t lastTS[hdr._nWorkers];
148 :     for (int i = 0; i <= hdr._nWorkers; i++) {
149 : jhr 4874 lastTS[i] = startTime;
150 : jhr 4868 }
151 :    
152 : jhr 4848 // read in the events
153 :     diderot::log::buffer buf;
154 :     uint64_t numEvents = 0;
155 :     for (int i = 0; i < numBufs; i++) {
156 :     ins.read (reinterpret_cast<char *>(&buf), sizeof(buf));
157 : jhr 4855 if (ins.fail()) {
158 : jhr 4848 std::cerr << "Error attempting to read buffer from \"" << name << "\"\n";
159 :     exit (1);
160 :     }
161 :     if (buf._next > diderot::log::buffer::NUM_EVENTS)
162 :     buf._next = diderot::log::buffer::NUM_EVENTS;
163 :     uint16_t worker = buf._worker;
164 :     if (log->_nWorkers < worker) {
165 :     std::cerr << "invalid worker ID " << worker << " for buffer (expected 0.."
166 :     << log->_nWorkers << ")\n";
167 :     exit (1);
168 :     }
169 :     for (int j = 0; j < buf._next; j++, numEvents++) {
170 :     diderot::log::event *ep = &(buf._log[j]);
171 : jhr 4868 uint64_t ts = get_timestamp(hdr._tsKind, &(ep->_ts));
172 :     if (ts < lastTS[worker]) {
173 :     std::cerr << "warning: timestamp for worker " << worker
174 : jhr 4871 << "; event #" << buf._seqNum << "." << j << "(=" << ep->_event << ") occurs "
175 : jhr 4868 << lastTS[worker]-ts << "ns too early\n";
176 : jhr 4879 dump_buffer (hdr, buf);
177 : jhr 4868 }
178 :     lastTS[worker] = ts;
179 :     log->_events.push_back (event (ts, worker, ep->_event, ep->_strand));
180 : jhr 4848 }
181 :     }
182 :     ins.close();
183 :    
184 : jhr 4875 if (noSort || (numEvents == 0)) {
185 : jhr 4853 return log;
186 :     }
187 :    
188 : jhr 4848 // sort the events by <timestamp, worker-id>
189 :     std::sort (log->_events.begin(), log->_events.end(), compare_events);
190 :    
191 :     // Adjust the timestamps to be relative to the start of the run
192 :     if (log->_events[0]._ts < startTime) {
193 :     std::cerr << "Warning: first event occurs " << (startTime - log->_events[0]._ts)
194 :     << " ns. before start time\n";
195 :     startTime = log->_events[0]._ts;
196 :     }
197 :     for (auto it = log->_events.begin(); it != log->_events.end(); ++it) {
198 :     it->_ts -= startTime;
199 :     }
200 :    
201 :     return log;
202 :    
203 :     }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0