Better isolation of server and clients system resources to allow starting the server...
[jack2.git] / common / JackTools.h
blob34141cc3e2236c90660a5ec56392424114ca5f39
1 /*
2 Copyright (C) 2006-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef __JackTools__
21 #define __JackTools__
23 #ifdef WIN32
24 #include <windows.h>
25 #else
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #endif
31 #ifdef __APPLE__
32 #include <sys/syslimits.h>
33 #endif
35 #include "jslist.h"
36 #include "driver_interface.h"
37 #include "JackCompilerDeps.h"
38 #include "JackError.h"
40 #include <string>
41 #include <algorithm>
42 #include <vector>
43 #include <iostream>
44 #include <fstream>
46 namespace Jack
49 /*!
50 \brief Utility functions.
53 struct SERVER_EXPORT JackTools
55 static int GetPID();
56 static int GetUID();
58 static char* UserDir();
59 static char* ServerDir ( const char* server_name, char* server_dir );
60 static const char* DefaultServerName();
61 static void CleanupFiles ( const char* server_name );
62 static int GetTmpdir();
63 static void RewriteName ( const char* name, char* new_name );
66 /*!
67 \brief Internal cient command line parser.
70 class SERVER_EXPORT JackArgParser
72 private:
74 std::string fArgString;
75 int fArgc;
76 std::vector<std::string> fArgv;
78 public:
80 JackArgParser ( const char* arg );
81 ~JackArgParser();
82 std::string GetArgString();
83 int GetNumArgv();
84 int GetArgc();
85 int GetArgv ( std::vector<std::string>& argv );
86 int GetArgv ( char** argv );
87 void DeleteArgv ( const char** argv );
88 void ParseParams ( jack_driver_desc_t* desc, JSList** param_list );
89 void FreeParams ( JSList* param_list );
92 /*!
93 \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
95 This template class allows to manipulate monitoring records, and automatically generate the GnuPlot config and data files.
96 Operations are RT safe because it uses fixed size data buffers.
97 You can set the number of measure points, and the number of records.
99 To use it :
100 - create a JackGnuPlotMonitor, you can use the data type you want.
101 - create a temporary array for your measure
102 - once you have filled this array with 'measure points' value, call write() to add it to the record
103 - once you've done with your measurment, just call save() to save your data file
105 You can also call SetPlotFile() to automatically generate '.plt' file from an options list.
109 template <class T> class JackGnuPlotMonitor
111 private:
112 uint32_t fMeasureCnt;
113 uint32_t fMeasurePoints;
114 uint32_t fMeasureId;
115 T* fCurrentMeasure;
116 T** fMeasureTable;
117 uint32_t fTablePos;
118 std::string fName;
120 public:
121 JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
123 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
125 fMeasureCnt = measure_cnt;
126 fMeasurePoints = measure_points;
127 fTablePos = 0;
128 fName = name;
129 fCurrentMeasure = new T[fMeasurePoints];
130 fMeasureTable = new T*[fMeasureCnt];
131 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
133 fMeasureTable[cnt] = new T[fMeasurePoints];
134 fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
138 ~JackGnuPlotMonitor()
140 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
142 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
143 delete[] fMeasureTable[cnt];
144 delete[] fMeasureTable;
145 delete[] fCurrentMeasure;
148 T AddNew ( T measure_point )
150 fMeasureId = 0;
151 return fCurrentMeasure[fMeasureId++] = measure_point;
154 uint32_t New()
156 return fMeasureId = 0;
159 T Add ( T measure_point )
161 return fCurrentMeasure[fMeasureId++] = measure_point;
164 uint32_t AddLast ( T measure_point )
166 fCurrentMeasure[fMeasureId] = measure_point;
167 fMeasureId = 0;
168 return Write();
171 uint32_t Write()
173 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
174 fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
175 if ( ++fTablePos == fMeasureCnt )
176 fTablePos = 0;
177 return fTablePos;
180 int Save ( std::string name = std::string ( "" ) )
182 std::string filename = ( name.empty() ) ? fName : name;
183 filename += ".log";
185 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
187 std::ofstream file ( filename.c_str() );
189 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
191 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
192 file << fMeasureTable[cnt][point] << " \t";
193 file << std::endl;
196 file.close();
197 return 0;
200 int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
201 std::string* field_names = NULL, uint32_t field_number = 0,
202 std::string name = std::string ( "" ) )
204 std::string title = ( name.empty() ) ? fName : name;
205 std::string plot_filename = title + ".plt";
206 std::string data_filename = title + ".log";
208 std::ofstream file ( plot_filename.c_str() );
210 file << "set multiplot" << std::endl;
211 file << "set grid" << std::endl;
212 file << "set title \"" << title << "\"" << std::endl;
214 for ( uint32_t i = 0; i < options_number; i++ )
215 file << options_list[i] << std::endl;
217 file << "plot ";
218 for ( uint32_t row = 1; row <= field_number; row++ )
220 file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
221 file << ( ( row < field_number ) ? ", " : "\n" );
224 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
226 file.close();
227 return 0;
232 #endif