diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 740c89dc0..befc59f4d 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -39,6 +39,7 @@ include/dfhack/VersionInfoFactory.h include/dfhack/VersionInfo.h include/dfhack/extra/MapExtras.h include/dfhack/extra/termutil.h +include/dfhack/extra/stopwatch.h include/dfhack/modules/Buildings.h include/dfhack/modules/Constructions.h include/dfhack/modules/Creatures.h diff --git a/library/include/dfhack/extra/stopwatch.h b/library/include/dfhack/extra/stopwatch.h new file mode 100644 index 000000000..1704178d4 --- /dev/null +++ b/library/include/dfhack/extra/stopwatch.h @@ -0,0 +1,45 @@ +// source: http://stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c +#ifndef LINUX_BUILD + #include +#else + #include + #include +#endif + +/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both + * windows and linux. */ + +uint64_t GetTimeMs64() +{ + #ifndef LINUX_BUILD + /* Windows */ + FILETIME ft; + LARGE_INTEGER li; + + /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it + * to a LARGE_INTEGER structure. */ + GetSystemTimeAsFileTime(&ft); + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; + + uint64_t ret = li.QuadPart; + ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */ + ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */ + + return ret; + #else + /* Linux */ + struct timeval tv; + + gettimeofday(&tv, NULL); + + uint64_t ret = tv.tv_usec; + /* Convert from micro seconds (10^-6) to milliseconds (10^-3) */ + ret /= 1000; + + /* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */ + ret += (tv.tv_sec * 1000); + + return ret; + #endif +} diff --git a/library/modules/Materials.cpp b/library/modules/Materials.cpp index f8e0399cc..212fb0aaf 100644 --- a/library/modules/Materials.cpp +++ b/library/modules/Materials.cpp @@ -226,8 +226,6 @@ bool API::ReadInorganicMaterials (vector & inorganic) } */ - - // good for now inline bool ReadNamesOnly(Process* p, uint32_t address, vector & names) { diff --git a/tools/playground/dfstatus.cpp b/tools/playground/dfstatus.cpp index 34ad52f9e..96f3bd542 100644 --- a/tools/playground/dfstatus.cpp +++ b/tools/playground/dfstatus.cpp @@ -23,9 +23,9 @@ #include #include using namespace std; -#define DFHACK_WANT_MISCUTILS #include #include +#include WINDOW *create_newwin(int height, int width, int starty, int startx); @@ -42,6 +42,9 @@ WINDOW *create_newwin(int height, int width, int starty, int startx); int32_t copperBars = 0; int32_t steelBars = 0; int32_t fuel = 0; + uint64_t start_time = 0; + uint64_t end_time = 0; + uint64_t total_time = 0; WINDOW *create_newwin(int height, int width, int starty, int startx){ WINDOW *local_win; @@ -64,7 +67,8 @@ WINDOW *create_newwin(int height, int width, int starty, int startx){ mvwprintw(local_win,5,22,"Copper Bars: %d", copperBars); mvwprintw(local_win,6,22,"Steel Bars: %d", steelBars); mvwprintw(local_win,8,22,"Fuel: %d", fuel); - + total_time += end_time - start_time; + mvwprintw(local_win,12,2,"Time used: %d ms, Time total: %d ms", end_time - start_time, total_time); wrefresh(local_win); // paint the screen and all components. @@ -79,21 +83,27 @@ int main() DFHack::Process * p; DFHack::ContextManager DFMgr("Memory.xml"); DFHack::Context * DF; + DFHack::Materials * Materials; try{ //is DF running? DF = DFMgr.getSingleContext(); - DF->Detach(); + DF->Attach(); + Materials = DF->getMaterials(); + Materials->ReadAllMaterials(); + DF->Resume(); } catch (exception& e){ cerr << e.what() << endl; return 1; } - //init and Attach + //init and Attach + ofstream file("dfstatus_errors.txt"); + streambuf* strm_buffer = cerr.rdbuf(); // save cerr's output buffer + cerr.rdbuf (file.rdbuf()); // redirect output into the file initscr(); //start curses. nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); - do{ drinkCount = 0; plantCount = 0; @@ -111,10 +121,11 @@ int main() //FILE * pFile; //pFile = fopen("dump.txt","w"); - - DF->Attach(); - DFHack::Materials * Materials = DF->getMaterials(); - Materials->ReadAllMaterials(); + start_time = GetTimeMs64(); + if(!DF->Suspend()) + { + break; + } //DFHack::Gui * Gui = DF->getGui(); @@ -161,7 +172,8 @@ int main() fprintf(pFile,"%5d: %12s - %64s - [%d]\n", idx, Items->getItemClass(itm.matdesc.itemType).c_str(), Items->getItemDescription(itm, Materials).c_str(), itm.quantity); }*/ } - DF->Detach(); + DF->Resume(); + end_time = GetTimeMs64(); //printf("%d - %d\n", (clock()/CLOCKS_PER_SEC),(clock()/CLOCKS_PER_SEC)%60); height = LINES; width = COLS; @@ -179,6 +191,8 @@ int main() } while(true); endwin(); /* End curses mode */ + cerr.rdbuf (strm_buffer); // restore old output buffer + file.close(); return 0; } \ No newline at end of file