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

SCM Repository

[diderot] Annotation of /branches/cuda/src/lib/common/logging.cxx
ViewVC logotype

Annotation of /branches/cuda/src/lib/common/logging.cxx

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jhr 5597 /*! \file logging.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 : adrianlehm 5619 #include "diderot/base.h"
14 :     #include "diderot/log-file.h"
15 :     #include "diderot/parallel.h"
16 : jhr 5597
17 :     #ifndef DIDEROT_ENABLE_LOGGING
18 :     # error expected DIDEROT_ENABLE_LOGGING to be defined
19 :     #endif
20 :    
21 :     namespace diderot {
22 :    
23 :     namespace log {
24 :    
25 :     file::file (std::string const &name, const diderot::scheduler *sched)
26 :     : _ofs(name), _nbuffers(sched->_numWorkers+1), _buffers(0)
27 :     {
28 :     if (! this->_ofs.good()) {
29 :     std::cerr << "unable to open logging output file \"" << name << "\"\n";
30 :     exit (1);
31 :     }
32 :    
33 :     // initialize the per-worker buffers
34 :     this->_buffers = new buffer* [this->_nbuffers];
35 :     for (int i = 0; i < this->_nbuffers; i++) {
36 :     this->_buffers[i] = new buffer;
37 :     this->_buffers[i]->_worker = i;
38 :     this->_buffers[i]->_next = 0;
39 :     this->_buffers[i]->_seqNum = 0;
40 :     }
41 :    
42 :     // initialize the file header
43 :     file_hdr hdr;
44 :     bzero (&hdr, sizeof(file_hdr));
45 :     hdr._magic = file_hdr::MAGIC;
46 :     hdr._version[0] = DIDEROT_LOG_VERSION_MAJOR;
47 :     hdr._version[1] = DIDEROT_LOG_VERSION_MINOR;
48 :     hdr._version[2] = DIDEROT_LOG_VERSION_PATCH;
49 :     hdr._version[3] = 0; // DIDEROT_LOG_VERSION_FAT
50 :     hdr._hdrSzB = sizeof(file_hdr);
51 :     hdr._eventSzB = sizeof(event);
52 :     hdr._bufSzB = buffer::SIZEB;
53 :     time_t tim = time(0);
54 :     ctime_r (&tim, hdr._date);
55 :     hdr._date[24] = '\0'; /* zero out '\n' */
56 :     set_time_stamp (&(hdr._startTime));
57 :     #if HAVE_MACH_ABSOLUTE_TIME
58 :     hdr._tsKind = TS_MACH_ABSOLUTE;
59 :     strncpy(hdr._clockName, "mach_absolute_time", sizeof(hdr._clockName)-1);
60 :     hdr._resolution = 1;
61 :     #elif HAVE_CLOCK_GETTIME
62 :     hdr._tsKind = TS_TIMESPEC;
63 :     #ifdef CLOCK_MONOTONIC
64 :     strncpy(hdr._clockName, "clock_gettime(CLOCK_MONOTONIC)", sizeof(hdr._clockName)-1);
65 :     #else
66 :     strncpy(hdr._clockName, "clock_gettime(CLOCK_REALTIME)", sizeof(hdr._clockName)-1);
67 :     #endif
68 :     struct timespec res;
69 :     #ifdef CLOCK_MONOTONIC
70 :     clock_getres(CLOCK_MONOTONIC, &res);
71 :     #else
72 :     clock_getres(CLOCK_REALTIME, &res);
73 :     #endif
74 :     hdr._resolution = res.tv_nsec;
75 :     #else
76 :     hdr._tsKind = TS_TIMEVAL;
77 :     strncpy(hdr._clockName, "gettimeofday", sizeof(hdr._clockName)-1);
78 :     hdr._resolution = 1000;
79 :     #endif
80 :     hdr._nNodes = sched->_numHWNodes;
81 :     hdr._nCores = sched->_numHWCores;
82 :     hdr._nWorkers = sched->_numWorkers;
83 :    
84 :     // write the header block
85 :     this->_ofs.write (reinterpret_cast<const char *>(&hdr), sizeof(hdr));
86 :     this->_ofs.flush ();
87 :    
88 :     // initialize the mutex
89 :     pthread_mutex_init (&this->_lock, nullptr);
90 :     }
91 :    
92 :     file::~file ()
93 :     {
94 :     // flush out any remaining buffers,
95 :     for (int i = 0; i < this->_nbuffers; i++) {
96 :     if (this->_buffers[i]->_next > 0) {
97 :     this->output_buffer(this->_buffers[i]);
98 :     }
99 :     }
100 :    
101 :     // close the file
102 :     this->_ofs.close();
103 :    
104 :     // free memory
105 :     for (int i = 0; i < this->_nbuffers; i++) {
106 :     delete[] this->_buffers[i];
107 :     }
108 :     delete[] this->_buffers;
109 :     }
110 :    
111 :     void file::output_buffer (buffer *buf)
112 :     {
113 :     pthread_mutex_lock (&this->_lock);
114 :     // write the buffer to the output file
115 :     this->_ofs.write (reinterpret_cast<const char *>(buf), sizeof(buffer));
116 :     // TODO: check for I/O error
117 :     pthread_mutex_unlock (&this->_lock);
118 :    
119 :     // reset buffer
120 :     buf->_next = 0;
121 :     buf->_seqNum++;
122 :     }
123 :    
124 :     } // namespace log
125 :    
126 :     } //namespace diderot

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