summaryrefslogtreecommitdiffstats
path: root/grapher/StapParser.cxx
blob: 803b5fb3b3102cd860926462dcab2b1e59612b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// systemtap grapher
// Copyright (C) 2009 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "StapParser.hxx"

#include <unistd.h>

#include <gtkmm/window.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <cstring>

#include <boost/algorithm/string.hpp>
#include <boost/range.hpp>

namespace systemtap
{
using namespace std;
using namespace std::tr1;

sigc::signal<void, pid_t>& childDiedSignal()
{
  static sigc::signal<void, pid_t> deathSignal;
  return deathSignal;
}

ParserList parsers;
  
sigc::signal<void>& parserListChangedSignal()
{
  static sigc::signal<void> listChangedSignal;
  return listChangedSignal;
}

vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range)
{
  using namespace boost;
  vector<string> result;
  split(result, range, is_any_of(","));
  return result;
}

void StapParser::parseData(shared_ptr<GraphDataBase> gdata,
                           int64_t time, const string& dataString)
{
  std::istringstream stream(dataString);
  shared_ptr<GraphData<double> > dblptr;
  shared_ptr<GraphData<string> > strptr;
  dblptr = dynamic_pointer_cast<GraphData<double> >(gdata);
  if (dblptr)
    {
      double data;
      stream >> data;
      dblptr->times.push_back(time);
      dblptr->data.push_back(data);
    }
  else if ((strptr = dynamic_pointer_cast<GraphData<string> >(gdata))
           != 0)
    {
      strptr->times.push_back(time);
      strptr->data.push_back(dataString);
    }
}

bool findTaggedValue(const string& src, const char* tag, string& result)
{
  using namespace boost;
  sub_range<const string> found = find_first(src, tag);
  if (found.empty())
    return false;
  result.insert(result.end(),found.end(), src.end());
  return true;
}

bool StapParser::ioCallback(Glib::IOCondition ioCondition)
{
  using namespace std;
  using std::tr1::shared_ptr;
  using namespace boost;
  if (ioCondition & Glib::IO_HUP)
    {
      if (_catchHUP)
        {
          childDiedSignal().emit(getPid());
          _ioConnection.disconnect();
          _errIoConnection.disconnect();
        }
      return true;
    }
  if ((ioCondition & Glib::IO_IN) == 0)
    return true;
  char buf[256];
  ssize_t bytes_read = 0;
  bytes_read = read(_inFd, buf, sizeof(buf) - 1);
  if (bytes_read <= 0)
    {
      childDiedSignal().emit(getPid());
      return true;
    }
  _buffer.append(buf, bytes_read);
  string::size_type ret = string::npos;
  while ((ret = _buffer.find(_lineEndChar)) != string::npos)
    {
      Glib::ustring dataString(_buffer, 0, ret);
      // %DataSet and %CSV declare a data set; all other
      // statements begin with the name of a data set.
      // Except %LineEnd :)
      sub_range<Glib::ustring> found;
      if (dataString[0] == '%')
        {
          if ((found = find_first(dataString, "%DataSet:")))
            {
              string setName;
              int hexColor;
              double scale;
              string style;
              istringstream stream(Glib::ustring(found.end(),
                                                 dataString.end()));
              stream >> setName >> scale >> std::hex >> hexColor
                     >> style;
              if (style == "bar" || style == "dot")
                {
                  std::tr1::shared_ptr<GraphData<double> >
                    dataSet(new GraphData<double>);
                  dataSet->name = setName;
                  if (style == "dot")
                    dataSet->style = &GraphStyleDot::instance;
                  dataSet->color[0] = (hexColor >> 16) / 255.0;
                  dataSet->color[1] = ((hexColor >> 8) & 0xff) / 255.0;
                  dataSet->color[2] = (hexColor & 0xff) / 255.0;
                  dataSet->scale = scale;
                  _dataSets.insert(std::make_pair(setName, dataSet));
                  getGraphData().push_back(dataSet);
                  graphDataSignal().emit();
                }
              else if (style == "discreet")
                {
                  std::tr1::shared_ptr<GraphData<string> >
                    dataSet(new GraphData<string>);
                  dataSet->name = setName;
                  dataSet->style = &GraphStyleEvent::instance;
                  dataSet->color[0] = (hexColor >> 16) / 255.0;
                  dataSet->color[1] = ((hexColor >> 8) & 0xff) / 255.0;
                  dataSet->color[2] = (hexColor & 0xff) / 255.0;
                  dataSet->scale = scale;
                  _dataSets.insert(std::make_pair(setName, dataSet));
                  getGraphData().push_back(dataSet);
                  graphDataSignal().emit();
                }
            }
          else if ((found = find_first(dataString, "%CSV:")))
            {
              vector<string> tokens
                = commaSplit(sub_range<Glib::ustring>(found.end(),
                                                      dataString.end()));
              for (vector<string>::iterator tokIter = tokens.begin(),
                     e = tokens.end();
                   tokIter != e;
                   ++tokIter)
                {
                  DataMap::iterator setIter = _dataSets.find(*tokIter);
                  if (setIter != _dataSets.end())
                    _csv.elements
                      .push_back(CSVData::Element(*tokIter,
                                                  setIter->second));
                }
            }
          else if ((found = find_first(dataString, "%LineEnd:")))
            {
              istringstream stream(Glib::ustring(found.end(),
                                                 dataString.end()));
              int charAsInt = 0;
              // parse hex and octal numbers too
              stream >> std::setbase(0) >> charAsInt;
              _lineEndChar = static_cast<char>(charAsInt);
            }
          else
            {
              cerr << "Unknown declaration " << dataString << endl;
            }
        }
      else
        {
          std::istringstream stream(dataString);
          string setName;
          stream >> setName;
          DataMap::iterator itr = _dataSets.find(setName);
          if (itr != _dataSets.end())
            {
              shared_ptr<GraphDataBase> gdata = itr->second;
              string decl;
              // Hack: scan from the beginning of dataString again
              if (findTaggedValue(dataString, "%Title:", decl))
                {
                  gdata->title = decl;
                }
              else if (findTaggedValue(dataString, "%XAxisTitle:", decl))
                {
                  gdata->xAxisText = decl;
                }
              else if (findTaggedValue(dataString, "%YAxisTitle:", decl))
                {
                  gdata->yAxisText = decl;
                }
              else if ((found = find_first(dataString, "%YMax:")))
                {
                  double ymax;
                  std::istringstream
                    stream(Glib::ustring(found.end(), dataString.end()));
                  stream >> ymax;
                  gdata->scale = ymax;
                }
              else
                {
                  if (!_csv.elements.empty())
                    {
                      vector<string> tokens = commaSplit(dataString);
                      int i = 0;
                      int64_t time;
                      vector<string>::iterator tokIter = tokens.begin();
                      std::istringstream timeStream(*tokIter++);
                      timeStream >> time;
                      for (vector<string>::iterator e = tokens.end();
                           tokIter != e;
                           ++tokIter, ++i)
                        {
                          parseData(_csv.elements[i].second, time,
                                    *tokIter);
                        }
                    }
                  else
                    {
                      int64_t time;
                      stringbuf data;
                      stream >> time;
                      stream.get(data, _lineEndChar);
                      parseData(itr->second, time, data.str());
                    }
                }
            }
        }
      _buffer.erase(0, ret + 1);
    }
  return true;
}

bool StapParser::errIoCallback(Glib::IOCondition ioCondition)
{
  using namespace std;
  if ((ioCondition & Glib::IO_IN) == 0)
    return true;
  char buf[256];
  ssize_t bytes_read = 0;
  bytes_read = read(_errFd, buf, sizeof(buf) - 1);
  if (bytes_read <= 0)
    {
      cerr << "StapParser: error reading from stderr!\n";
      return true;
    }
  if (write(STDERR_FILENO, buf, bytes_read) < 0)
    ;
  return true;
}

void StapParser::initIo(int inFd, int errFd, bool catchHUP)
{
  _inFd = inFd;
  _errFd = errFd;
  _catchHUP = catchHUP;
  Glib::IOCondition inCond = Glib::IO_IN;
  if (catchHUP)
    inCond |= Glib::IO_HUP;
  if (_errFd >= 0)
    {    
      _errIoConnection = Glib::signal_io()
        .connect(sigc::mem_fun(*this, &StapParser::errIoCallback),
                 _errFd, Glib::IO_IN);
    }
  _ioConnection = Glib::signal_io()
    .connect(sigc::mem_fun(*this, &StapParser::ioCallback),
             _inFd, inCond);

}

void StapParser::disconnect()
{
  if (_ioConnection.connected())
    _ioConnection.disconnect();
  if (_errIoConnection.connected())
    _errIoConnection.disconnect();
  if (_inFd >= 0)
    {
      close(_inFd);
      _inFd = -1;
    }
  if (_errFd >= 0)
    {
      close(_errFd);
      _errFd = -1;
    }
}
}