00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "Python.h"
00032 #include "structmember.h"
00033
00034 #include "BundleCollector.h"
00035 #include "pydtn/WrapNode.h"
00036 #include "pydtn/parsers.h"
00037 #include "simlpy/interpreter_hooks.h"
00038 #include "simlpy/Clock.h"
00039
00040 #include <iostream>
00041 #include <fstream>
00042
00102 #ifndef PyMODINIT_FUNC
00105 #define PyMODINIT_FUNC extern "C" void
00106 #endif
00107
00109 static std::ostream* _stream = &(std::cout);
00110
00114 static char storeprofile_setup_doc[] = "Storage profiling configuration.\n\
00115 \n\
00116 This method should be called if stable storage profiling is desired.\n\
00117 \n\
00118 Calling syntax:\n\
00119 \n\
00120 pydtn.storeprofile.setup(<fname>)\n\
00121 \n\
00122 <fname> is either a filename or one of 'stdout' or 'stderr'.\n\
00123 In the former case, the profiling information is written\n\
00124 to the file named. In the latter cases, the information is\n\
00125 written to the appropriate stream.\n";
00126
00134 static PyObject*
00135 storeprofile_setup( PyObject* self, PyObject* args )
00136 {
00137 const char* fname;
00138 if ( ! PyArg_ParseTuple(args, "s", &fname) )
00139 {
00140 std::cerr << "Incorrect argument list" << std::endl;
00141 return 0;
00142 }
00143 std::string fnstr( fname );
00144
00145 if ( "stdout" == fnstr )
00146 {
00147 _stream = &(std::cout);
00148 }
00149 else if ( "stderr" == fnstr )
00150 {
00151 _stream = &(std::cerr);
00152 }
00153 else
00154 {
00155 _stream = new std::ofstream(fname);
00156 }
00157
00158 Py_INCREF(Py_None);
00159 return Py_None;
00160 }
00161
00165 static char storeprofile_collect_doc[] = "Add profiling to a node.\n\
00166 \n\
00167 Calling syntax:\n\
00168 \n\
00169 pydtn.storeprofile.collect(<n>)\n\
00170 \n\
00171 <n> is a node for which we want to collect a profile.\n";
00172
00179 static PyObject*
00180 storeprofile_collect( PyObject* self, PyObject* args )
00181 {
00182 if ( ! PyTuple_Check( args ) )
00183 {
00184 PyErr_SetString(PyExc_TypeError,"collect expects a list of arguments");
00185 return 0;
00186 }
00187 if ( 0 == PyTuple_Size( args ) )
00188 {
00189 PyErr_SetString(PyExc_TypeError,
00190 "collect expects a non-empty argument list");
00191 return 0;
00192 }
00193 PyObject* p = PyTuple_GetItem( args, 0 );
00194 WrapNode* wn = parse_node(p);
00195 if ( 0 == wn )
00196 {
00197 PyErr_SetString(PyExc_TypeError,
00198 "collect expects a node");
00199 return 0;
00200 }
00201 wn->addCollector( new StoreProfile::BundleCollector( *_stream ) );
00202 Py_INCREF(Py_None);
00203 return Py_None;
00204 }
00205
00208 static PyMethodDef ExampleMethods[] =
00209 {
00210 {"setup",storeprofile_setup,METH_VARARGS,storeprofile_setup_doc},
00211 {"collect",storeprofile_collect,METH_VARARGS,storeprofile_collect_doc},
00212 {0,0,0,0}
00213 };
00214
00218 PyMODINIT_FUNC
00219 init_storeprofile(void)
00220 {
00221 (void) Py_InitModule("_storeprofile",ExampleMethods);
00222 }
00223