summaryrefslogtreecommitdiff
path: root/common/JackTools.h
blob: 820f91bae9b5d854ad461eb00becc57a3b91a5ed (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
/*
  Copyright (C) 2006-2008 Grame

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#ifndef __JackTools__
#define __JackTools__

#ifdef WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#endif

#ifdef __APPLE__
#include <sys/syslimits.h>
#endif

#include "jslist.h"
#include "driver_interface.h"
#include "JackCompilerDeps.h"
#include "JackError.h"

#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>

namespace Jack
{

    /*!
    \brief Utility functions.
    */

    struct SERVER_EXPORT JackTools
    {
        static int GetPID();
        static int GetUID();

        static char* UserDir();
        static char* ServerDir ( const char* server_name, char* server_dir );
        static const char* DefaultServerName();
        static void CleanupFiles ( const char* server_name );
        static int GetTmpdir();
        static void RewriteName ( const char* name, char* new_name );
    };

    /*!
    \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)

    This template class allows to manipulate monitoring records, and automatically generate the GnuPlot config and data files.
    Operations are RT safe because it uses fixed size data buffers.
    You can set the number of measure points, and the number of records.

    To use it :
    - create a JackGnuPlotMonitor, you can use the data type you want.
    - create a temporary array for your measure
    - once you have filled this array with 'measure points' value, call write() to add it to the record
    - once you've done with your measurment, just call save() to save your data file

    You can also call SetPlotFile() to automatically generate '.plt' file from an options list.

    */

    template <class T> class JackGnuPlotMonitor
    {
        private:
            uint32_t fMeasureCnt;
            uint32_t fMeasurePoints;
            uint32_t fMeasureId;
            T* fCurrentMeasure;
            T** fMeasureTable;
            uint32_t fTablePos;
            std::string fName;

        public:
            JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
            {
                jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );

                fMeasureCnt = measure_cnt;
                fMeasurePoints = measure_points;
                fTablePos = 0;
                fName = name;
                fCurrentMeasure = new T[fMeasurePoints];
                fMeasureTable = new T*[fMeasureCnt];
                for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
                {
                    fMeasureTable[cnt] = new T[fMeasurePoints];
                    fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
                }
            }

            ~JackGnuPlotMonitor()
            {
                jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );

                for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
                    delete[] fMeasureTable[cnt];
                delete[] fMeasureTable;
                delete[] fCurrentMeasure;
            }

            T AddNew ( T measure_point )
            {
				fMeasureId = 0;
				return fCurrentMeasure[fMeasureId++] = measure_point;
            }

			uint32_t New()
			{
				return fMeasureId = 0;
			}

            T Add ( T measure_point )
            {
				return fCurrentMeasure[fMeasureId++] = measure_point;
            }

            uint32_t AddLast ( T measure_point )
            {
            	fCurrentMeasure[fMeasureId] = measure_point;
            	fMeasureId = 0;
            	return Write();
            }

            uint32_t Write()
            {
                for ( uint32_t point = 0; point < fMeasurePoints; point++ )
                    fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
                if ( ++fTablePos == fMeasureCnt )
                    fTablePos = 0;
                return fTablePos;
            }

            int Save ( std::string name = std::string ( "" ) )
            {
                std::string filename = ( name.empty() ) ? fName : name;
                filename += ".log";

                jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );

                std::ofstream file ( filename.c_str() );

                for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
                {
                    for ( uint32_t point = 0; point < fMeasurePoints; point++ )
                        file << fMeasureTable[cnt][point] << " \t";
                    file << std::endl;
                }

                file.close();
                return 0;
            }

            int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
                              std::string* field_names = NULL, uint32_t field_number = 0,
                              std::string name = std::string ( "" ) )
            {
                std::string title = ( name.empty() ) ? fName : name;
                std::string plot_filename = title + ".plt";
                std::string data_filename = title + ".log";

                std::ofstream file ( plot_filename.c_str() );

                file << "set multiplot" << std::endl;
                file << "set grid" << std::endl;
                file << "set title \"" << title << "\"" << std::endl;

                for ( uint32_t i = 0; i < options_number; i++ )
                    file << options_list[i] << std::endl;

                file << "plot ";
                for ( uint32_t row = 1; row <= field_number; row++ )
                {
                    file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
                    file << ( ( row < field_number ) ? ", " : "\n" );
                }

                jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );

                file.close();
                return 0;
            }
    };
    
    void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
    void PrintLoadError(const char* so_name);
    
}

#endif