From 85b19784086faa7676d030f03bfc4801705c180f Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Sun, 3 Mar 2024 21:39:53 +0300 Subject: [PATCH] Use 64-bit type to represent microseconds. int and long are 32-bit on Windows, causing overflows. --- 3d-viewer/3d_canvas/board_adapter.cpp | 2 +- 3d-viewer/3d_canvas/create_layer_items.cpp | 4 ++-- 3d-viewer/3d_canvas/eda_3d_canvas.cpp | 4 ++-- 3d-viewer/3d_canvas/eda_3d_canvas.h | 2 +- 3d-viewer/3d_rendering/opengl/create_scene.cpp | 2 +- .../3d_rendering/raytracing/create_scene.cpp | 10 +++++----- .../3d_rendering/raytracing/render_3d_raytrace.h | 2 +- libs/core/include/core/profile.h | 3 ++- libs/core/profile.cpp | 15 ++++++++------- pcbnew/exporters/step/exporter_step.cpp | 2 +- pcbnew/files.cpp | 4 ++-- pcbnew/tools/zone_filler_tool.cpp | 2 +- 12 files changed, 27 insertions(+), 25 deletions(-) diff --git a/3d-viewer/3d_canvas/board_adapter.cpp b/3d-viewer/3d_canvas/board_adapter.cpp index 207699e727..cf948be7c4 100644 --- a/3d-viewer/3d_canvas/board_adapter.cpp +++ b/3d-viewer/3d_canvas/board_adapter.cpp @@ -530,7 +530,7 @@ void BOARD_ADAPTER::InitSettings( REPORTER* aStatusReporter, REPORTER* aWarningR m_boardBoundingBox = BBOX_3D( boardMin, boardMax ); #ifdef PRINT_STATISTICS_3D_VIEWER - unsigned stats_startCreateBoardPolyTime = GetRunningMicroSecs(); + int64_t stats_startCreateBoardPolyTime = GetRunningMicroSecs(); #endif if( aStatusReporter ) diff --git a/3d-viewer/3d_canvas/create_layer_items.cpp b/3d-viewer/3d_canvas/create_layer_items.cpp index f0ec777459..7bcedf2cf1 100644 --- a/3d-viewer/3d_canvas/create_layer_items.cpp +++ b/3d-viewer/3d_canvas/create_layer_items.cpp @@ -155,9 +155,9 @@ void BOARD_ADAPTER::createLayers( REPORTER* aStatusReporter ) // https://github.com/KiCad/kicad-source-mirror/blob/master/3d-viewer/3d_draw.cpp#L692 #ifdef PRINT_STATISTICS_3D_VIEWER - unsigned stats_startCopperLayersTime = GetRunningMicroSecs(); + int64_t stats_startCopperLayersTime = GetRunningMicroSecs(); - unsigned start_Time = stats_startCopperLayersTime; + int64_t start_Time = stats_startCopperLayersTime; #endif PCB_LAYER_ID cu_seq[MAX_CU_LAYERS]; diff --git a/3d-viewer/3d_canvas/eda_3d_canvas.cpp b/3d-viewer/3d_canvas/eda_3d_canvas.cpp index 6c2a589a7d..cbd252c4f0 100644 --- a/3d-viewer/3d_canvas/eda_3d_canvas.cpp +++ b/3d-viewer/3d_canvas/eda_3d_canvas.cpp @@ -369,7 +369,7 @@ void EDA_3D_CANVAS::DoRePaint() wxString err_messages; INFOBAR_REPORTER warningReporter( m_parentInfoBar ); STATUSBAR_REPORTER activityReporter( m_parentStatusBar, EDA_3D_VIEWER_STATUSBAR::ACTIVITY ); - unsigned start_time = GetRunningMicroSecs(); + int64_t start_time = GetRunningMicroSecs(); // "Makes the OpenGL state that is represented by the OpenGL rendering // context context current, i.e. it will be used by all subsequent OpenGL calls. @@ -461,7 +461,7 @@ void EDA_3D_CANVAS::DoRePaint() if( m_camera_is_moving ) { - const unsigned curtime_delta = GetRunningMicroSecs() - m_strtime_camera_movement; + const int64_t curtime_delta = GetRunningMicroSecs() - m_strtime_camera_movement; curtime_delta_s = (curtime_delta / 1e6) * m_camera_moving_speed; m_camera.Interpolate( curtime_delta_s ); diff --git a/3d-viewer/3d_canvas/eda_3d_canvas.h b/3d-viewer/3d_canvas/eda_3d_canvas.h index d34c74765c..ed6fd21efe 100644 --- a/3d-viewer/3d_canvas/eda_3d_canvas.h +++ b/3d-viewer/3d_canvas/eda_3d_canvas.h @@ -305,7 +305,7 @@ private: bool m_render_pivot; // Render the pivot while camera moving float m_camera_moving_speed; // 1.0f will be 1:1 - unsigned m_strtime_camera_movement; // Ticktime of camera movement start + int64_t m_strtime_camera_movement; // Ticktime of camera movement start bool m_animation_enabled; // Camera animation enabled int m_moving_speed_multiplier; // Camera animation speed multiplier option diff --git a/3d-viewer/3d_rendering/opengl/create_scene.cpp b/3d-viewer/3d_rendering/opengl/create_scene.cpp index 0ed0f88bea..9492541b9c 100644 --- a/3d-viewer/3d_rendering/opengl/create_scene.cpp +++ b/3d-viewer/3d_rendering/opengl/create_scene.cpp @@ -454,7 +454,7 @@ void RENDER_3D_OPENGL::reload( REPORTER* aStatusReporter, REPORTER* aWarningRepo OBJECT_2D_STATS::Instance().ResetStats(); - unsigned stats_startReloadTime = GetRunningMicroSecs(); + int64_t stats_startReloadTime = GetRunningMicroSecs(); m_boardAdapter.InitSettings( aStatusReporter, aWarningReporter ); diff --git a/3d-viewer/3d_rendering/raytracing/create_scene.cpp b/3d-viewer/3d_rendering/raytracing/create_scene.cpp index 08be15b13b..d0384e8d23 100644 --- a/3d-viewer/3d_rendering/raytracing/create_scene.cpp +++ b/3d-viewer/3d_rendering/raytracing/create_scene.cpp @@ -368,7 +368,7 @@ void RENDER_3D_RAYTRACE::Reload( REPORTER* aStatusReporter, REPORTER* aWarningRe OBJECT_2D_STATS::Instance().ResetStats(); OBJECT_3D_STATS::Instance().ResetStats(); - unsigned stats_startReloadTime = GetRunningMicroSecs(); + int64_t stats_startReloadTime = GetRunningMicroSecs(); if( !aOnlyLoadCopperAndShapes ) { @@ -771,8 +771,8 @@ void RENDER_3D_RAYTRACE::Reload( REPORTER* aStatusReporter, REPORTER* aWarningRe } #ifdef PRINT_STATISTICS_3D_VIEWER - unsigned stats_endConvertTime = GetRunningMicroSecs(); - unsigned stats_startLoad3DmodelsTime = stats_endConvertTime; + int64_t stats_endConvertTime = GetRunningMicroSecs(); + int64_t stats_startLoad3DmodelsTime = stats_endConvertTime; #endif if( aStatusReporter ) @@ -781,7 +781,7 @@ void RENDER_3D_RAYTRACE::Reload( REPORTER* aStatusReporter, REPORTER* aWarningRe load3DModels( m_objectContainer, aOnlyLoadCopperAndShapes ); #ifdef PRINT_STATISTICS_3D_VIEWER - unsigned stats_endLoad3DmodelsTime = GetRunningMicroSecs(); + int64_t stats_endLoad3DmodelsTime = GetRunningMicroSecs(); #endif if( !aOnlyLoadCopperAndShapes ) @@ -955,7 +955,7 @@ void RENDER_3D_RAYTRACE::Reload( REPORTER* aStatusReporter, REPORTER* aWarningRe if( aStatusReporter ) { // Calculation time in seconds - double calculation_time = (double) GetRunningMicroSecs() - stats_startReloadTime / 1e6; + double calculation_time = (double) ( GetRunningMicroSecs() - stats_startReloadTime ) / 1e6; aStatusReporter->Report( wxString::Format( _( "Reload time %.3f s" ), calculation_time ) ); } diff --git a/3d-viewer/3d_rendering/raytracing/render_3d_raytrace.h b/3d-viewer/3d_rendering/raytracing/render_3d_raytrace.h index 115fc9c603..3e7396f1e9 100644 --- a/3d-viewer/3d_rendering/raytracing/render_3d_raytrace.h +++ b/3d-viewer/3d_rendering/raytracing/render_3d_raytrace.h @@ -154,7 +154,7 @@ private: RT_RENDER_STATE m_renderState; /// Time that the render starts - unsigned long int m_renderStartTime; + int64_t m_renderStartTime; /// Save the number of blocks progress of the render size_t m_blockRenderProgressCount; diff --git a/libs/core/include/core/profile.h b/libs/core/include/core/profile.h index 644ea71e8a..76744959eb 100644 --- a/libs/core/include/core/profile.h +++ b/libs/core/include/core/profile.h @@ -36,6 +36,7 @@ #include #include #include +#include /** * A small class to help profiling. @@ -216,7 +217,7 @@ private: * differences between two calls. * @author Dick Hollenbeck */ -unsigned GetRunningMicroSecs(); +int64_t GetRunningMicroSecs(); /** diff --git a/libs/core/profile.cpp b/libs/core/profile.cpp index b65f75352d..906f45d71b 100644 --- a/libs/core/profile.cpp +++ b/libs/core/profile.cpp @@ -24,34 +24,35 @@ #include +#include #if defined( _WIN32 ) #define WIN32_LEAN_AND_MEAN 1 #include -unsigned GetRunningMicroSecs() +int64_t GetRunningMicroSecs() { FILETIME now; GetSystemTimeAsFileTime( &now ); - unsigned long long t = ( UINT64( now.dwHighDateTime ) << 32 ) + now.dwLowDateTime; + uint64_t t = ( UINT64( now.dwHighDateTime ) << 32 ) + now.dwLowDateTime; t /= 10; - return unsigned( t ); + return int64_t( t ); } #elif defined( HAVE_CLOCK_GETTIME ) #include -unsigned GetRunningMicroSecs() +int64_t GetRunningMicroSecs() { struct timespec now; clock_gettime( CLOCK_MONOTONIC, &now ); - unsigned usecs = ( (unsigned) now.tv_nsec ) / 1000 + ( (unsigned) now.tv_sec ) * 1000000; + int64_t usecs = (int64_t) now.tv_sec * 1000000 + now.tv_nsec / 1000; // unsigned msecs = (now.tv_nsec / (1000*1000)) + now.tv_sec * 1000; return usecs; @@ -61,13 +62,13 @@ unsigned GetRunningMicroSecs() #elif defined( HAVE_GETTIMEOFDAY_FUNC ) #include -unsigned GetRunningMicroSecs() +int64_t GetRunningMicroSecs() { timeval tv; gettimeofday( &tv, 0 ); - return ( tv.tv_sec * 1000000 ) + tv.tv_usec; + return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec; } #endif diff --git a/pcbnew/exporters/step/exporter_step.cpp b/pcbnew/exporters/step/exporter_step.cpp index 7974b2dd3b..d9b0ada7bf 100644 --- a/pcbnew/exporters/step/exporter_step.cpp +++ b/pcbnew/exporters/step/exporter_step.cpp @@ -493,7 +493,7 @@ void EXPORTER_STEP::calculatePcbThickness() bool EXPORTER_STEP::Export() { // Display the export time, for statistics - unsigned stats_startExportTime = GetRunningMicroSecs(); + int64_t stats_startExportTime = GetRunningMicroSecs(); // setup opencascade message log Message::DefaultMessenger()->RemovePrinters( STANDARD_TYPE( Message_PrinterOStream ) ); diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp index e130efe429..66617d7a42 100644 --- a/pcbnew/files.cpp +++ b/pcbnew/files.cpp @@ -678,14 +678,14 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in #if USE_INSTRUMENTATION // measure the time to load a BOARD. - unsigned startTime = GetRunningMicroSecs(); + int64_t startTime = GetRunningMicroSecs(); #endif pi->SetProgressReporter( &progressReporter ); loadedBoard = pi->LoadBoard( fullFileName, nullptr, &props, &Prj() ); #if USE_INSTRUMENTATION - unsigned stopTime = GetRunningMicroSecs(); + int64_t stopTime = GetRunningMicroSecs(); printf( "PCB_IO::Load(): %u usecs\n", stopTime - startTime ); #endif } diff --git a/pcbnew/tools/zone_filler_tool.cpp b/pcbnew/tools/zone_filler_tool.cpp index c4a3e22cbf..55cc764a56 100644 --- a/pcbnew/tools/zone_filler_tool.cpp +++ b/pcbnew/tools/zone_filler_tool.cpp @@ -211,7 +211,7 @@ int ZONE_FILLER_TOOL::ZoneFillDirty( const TOOL_EVENT& aEvent ) if( m_fillInProgress ) return 0; - unsigned startTime = GetRunningMicroSecs(); + int64_t startTime = GetRunningMicroSecs(); m_fillInProgress = true; m_dirtyZoneIDs.clear();