Init engine fields, cleanup.
[jack2.git] / common / JackTools.h
blobf7d5ce1e932ac87b26dce31dd902abdecbc85b0c
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 #define DIR_SEPARATOR '\\'
26 #else
27 #define DIR_SEPARATOR '/'
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #endif
34 #ifdef __APPLE__
35 #include <sys/syslimits.h>
36 #endif
38 #include "jslist.h"
39 #include "driver_interface.h"
40 #include "JackCompilerDeps.h"
41 #include "JackError.h"
42 #include "JackException.h"
44 #include <string>
45 #include <algorithm>
46 #include <vector>
47 #include <iostream>
48 #include <fstream>
50 namespace Jack
53 /*!
54 \brief Utility functions.
57 struct SERVER_EXPORT JackTools
59 static int GetPID();
60 static int GetUID();
62 static void KillServer();
64 static int MkDir(const char* path);
65 static char* UserDir();
66 static char* ServerDir ( const char* server_name, char* server_dir );
67 static const char* DefaultServerName();
68 static void CleanupFiles ( const char* server_name );
69 static int GetTmpdir();
70 static void RewriteName ( const char* name, char* new_name );
72 static void ThrowJackNetException();
75 /*!
76 \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
78 This template class allows to manipulate monitoring records, and automatically generate the GnuPlot config and data files.
79 Operations are RT safe because it uses fixed size data buffers.
80 You can set the number of measure points, and the number of records.
82 To use it :
83 - create a JackGnuPlotMonitor, you can use the data type you want.
84 - create a temporary array for your measure
85 - once you have filled this array with 'measure points' value, call write() to add it to the record
86 - once you've done with your measurment, just call save() to save your data file
88 You can also call SetPlotFile() to automatically generate '.plt' file from an options list.
92 template <class T> class JackGnuPlotMonitor
94 private:
95 uint32_t fMeasureCnt;
96 uint32_t fMeasurePoints;
97 uint32_t fMeasureId;
98 T* fCurrentMeasure;
99 T** fMeasureTable;
100 uint32_t fTablePos;
101 std::string fName;
103 public:
104 JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
106 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
108 fMeasureCnt = measure_cnt;
109 fMeasurePoints = measure_points;
110 fTablePos = 0;
111 fName = name;
112 fCurrentMeasure = new T[fMeasurePoints];
113 fMeasureTable = new T*[fMeasureCnt];
114 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
116 fMeasureTable[cnt] = new T[fMeasurePoints];
117 fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
121 ~JackGnuPlotMonitor()
123 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
125 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
126 delete[] fMeasureTable[cnt];
127 delete[] fMeasureTable;
128 delete[] fCurrentMeasure;
131 T AddNew ( T measure_point )
133 fMeasureId = 0;
134 return fCurrentMeasure[fMeasureId++] = measure_point;
137 uint32_t New()
139 return fMeasureId = 0;
142 T Add ( T measure_point )
144 return fCurrentMeasure[fMeasureId++] = measure_point;
147 uint32_t AddLast ( T measure_point )
149 fCurrentMeasure[fMeasureId] = measure_point;
150 fMeasureId = 0;
151 return Write();
154 uint32_t Write()
156 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
157 fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
158 if ( ++fTablePos == fMeasureCnt )
159 fTablePos = 0;
160 return fTablePos;
163 int Save ( std::string name = std::string ( "" ) )
165 std::string filename = ( name.empty() ) ? fName : name;
166 filename += ".log";
168 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
170 std::ofstream file ( filename.c_str() );
172 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
174 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
175 file << fMeasureTable[cnt][point] << " \t";
176 file << std::endl;
179 file.close();
180 return 0;
183 int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
184 std::string* field_names = NULL, uint32_t field_number = 0,
185 std::string name = std::string ( "" ) )
187 std::string title = ( name.empty() ) ? fName : name;
188 std::string plot_filename = title + ".plt";
189 std::string data_filename = title + ".log";
191 std::ofstream file ( plot_filename.c_str() );
193 file << "set multiplot" << std::endl;
194 file << "set grid" << std::endl;
195 file << "set title \"" << title << "\"" << std::endl;
197 for ( uint32_t i = 0; i < options_number; i++ )
198 file << options_list[i] << std::endl;
200 file << "plot ";
201 for ( uint32_t row = 1; row <= field_number; row++ )
203 file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
204 file << ( ( row < field_number ) ? ", " : "\n" );
207 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
209 file.close();
210 return 0;
214 void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
215 void PrintLoadError(const char* so_name);
219 #endif