NSIS-ka
A free C++ implementation of NSIS protocols

root/natfw-nslp/trunk/src/benchmark_journal.cpp @ 2273

Revision 2273, 3.0 KB (checked in by stud-matfried, 7 years ago)

Renamed the benchmark class to benchmark_journal.
We will need the name "benchmark" later.

  • Property svn:keywords set to Id HeadURL
Line 
1/*
2 * The benchmark class.
3 *
4 * $Id$
5 * $HeadURL$
6 */
7#include <fstream>
8#include <cstring>
9#include <pthread.h>
10
11#include "benchmark_journal.h"
12
13using namespace natfw;
14
15
16/**
17 * Human readable measuring point names, used in write_header().
18 */
19const char *
20benchmark_journal::mp_names[benchmark_journal::HIGHEST_VALID_ID+1] = {
21        "INVALID_ID",
22        "PRE_PROCESSING",
23        "POST_PROCESSING",
24        "PRE_MAPPING",
25        "POST_MAPPING",
26        "PRE_SERIALIZE",
27        "POST_SERIALIZE",
28        "PRE_DESERIALIZE",
29        "POST_DESERIALIZE",
30        "PRE_DISPATCHER",
31        "POST_DISPATCHER",
32        "PRE_SESSION",
33        "POST_SESSION"
34};
35
36
37/**
38 * Constructor to create a journal of the given size.
39 *
40 * The journal is initialized to avoid page faults during usage.
41 * If a filename is given (!= the empty string), the journal is written to
42 * that file as soon as the journal is full.
43 *
44 * @param journal_size the size of the in-memory journal
45 * @param filename the name of the file to write the journal to
46 */
47benchmark_journal::benchmark_journal(
48                int journal_size, const std::string &filename)
49                : journal_size(journal_size), journal_pos(0),
50                  filename(filename), disable_journal(false) {
51
52        journal = new measuring_point_t[journal_size];
53        memset(journal, 0,
54                sizeof(benchmark_journal::measuring_point_t) * journal_size);
55
56        /*
57         * Initialize the mutex. We use a recursive mutex to make calling
58         * write_journal() from add() easier.
59         */
60        pthread_mutexattr_t mutex_attr;
61
62        pthread_mutexattr_init(&mutex_attr);
63        pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
64        pthread_mutex_init(&mutex, &mutex_attr);
65
66        pthread_mutexattr_destroy(&mutex_attr);
67}
68
69
70benchmark_journal::~benchmark_journal() {
71        delete journal;
72        pthread_mutex_destroy(&mutex);
73}
74
75
76void benchmark_journal::write_journal(const std::string &filename) {
77        std::ofstream out(filename.c_str());
78
79        if ( ! out ) {
80                std::cerr << "Error opening journal file `" + filename + "'\n";
81                return;
82        }
83
84        try {
85                this->write_journal(out);
86        }
87        catch ( ... ) {
88                std::cerr << "Error writing journal" << std::endl;
89        }
90
91        out.close();
92}
93
94
95/**
96 * Write the journal to the given stream.
97 *
98 * Note that the caller is responsible for closing the stream.
99 */
100void benchmark_journal::write_journal(std::ostream &out) {
101        pthread_mutex_lock(&mutex);
102
103        write_header(out);
104
105        for ( int i = 0; i < journal_size
106                                && journal[i].point != INVALID_ID; i++ )
107                out << (int) journal[i].point << ' '
108                        << journal[i].thread_id << ' '
109                        << journal[i].timestamp.tv_sec << ' '
110                        << journal[i].timestamp.tv_nsec << std::endl;
111
112        pthread_mutex_unlock(&mutex);
113}
114
115
116/**
117 * Write a header to the journal documenting the measuring points.
118 */
119void benchmark_journal::write_header(std::ostream &out) {
120        time_t t;
121
122        time(&t);
123
124        out << "# Benchmark Journal, created " << ctime(&t) << std::endl;
125        out << "# $Id$" << std::endl;
126        out << "# Format: <measuring point ID> <Thread ID>"
127                " <seconds> <nano seconds>" << std::endl;
128        out << "# Measuring points:" << std::endl;
129
130        for (int i = 0; i <= HIGHEST_VALID_ID; i++)
131                out << "# \t" << i << " " << mp_names[i] << std::endl;
132
133        out << "#" << std::endl;
134}
135
136// EOF
Note: See TracBrowser for help on using the browser.