LCOV - code coverage report
Current view: top level - xenolith/core/platform/linux - XLCorePlatform.cc (source / functions) Hit Total Coverage
Test: coverage.info Lines: 18 26 69.2 %
Date: 2024-05-12 00:16:13 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**
       2             :  Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
       3             : 
       4             :  Permission is hereby granted, free of charge, to any person obtaining a copy
       5             :  of this software and associated documentation files (the "Software"), to deal
       6             :  in the Software without restriction, including without limitation the rights
       7             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       8             :  copies of the Software, and to permit persons to whom the Software is
       9             :  furnished to do so, subject to the following conditions:
      10             : 
      11             :  The above copyright notice and this permission notice shall be included in
      12             :  all copies or substantial portions of the Software.
      13             : 
      14             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      15             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      16             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      17             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      18             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      19             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      20             :  THE SOFTWARE.
      21             :  **/
      22             : 
      23             : #include "XLCorePlatform.h"
      24             : 
      25             : #if LINUX
      26             : 
      27             : #include "SPPlatformUnistd.h"
      28             : 
      29             : namespace STAPPLER_VERSIONIZED stappler::xenolith::platform {
      30             : 
      31          16 : static uint64_t getStaticMinFrameTime() {
      32          16 :         return 1000'000 / 60;
      33             : }
      34             : 
      35          16 : static clockid_t getClockSource() {
      36             :         struct timespec ts;
      37             : 
      38          16 :         auto minFrameNano = (getStaticMinFrameTime() * 1000) / 5; // clock should have at least 1/5 frame resolution
      39          16 :         if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) == 0) {
      40          16 :                 if (ts.tv_sec == 0 && uint64_t(ts.tv_nsec) < minFrameNano) {
      41           0 :                         return CLOCK_MONOTONIC_COARSE;
      42             :                 }
      43             :         }
      44             : 
      45          16 :         if (clock_getres(CLOCK_MONOTONIC, &ts) == 0) {
      46          16 :                 if (ts.tv_sec == 0 && uint64_t(ts.tv_nsec) < minFrameNano) {
      47          16 :                         return CLOCK_MONOTONIC;
      48             :                 }
      49             :         }
      50             : 
      51           0 :         if (clock_getres(CLOCK_MONOTONIC_RAW, &ts) == 0) {
      52           0 :                 if (ts.tv_sec == 0 && uint64_t(ts.tv_nsec) < minFrameNano) {
      53           0 :                         return CLOCK_MONOTONIC_RAW;
      54             :                 }
      55             :         }
      56             : 
      57           0 :         return CLOCK_MONOTONIC;
      58             : }
      59             : 
      60    83818808 : uint64_t clock(core::ClockType type) {
      61    83818808 :         static clockid_t ClockSource = getClockSource();
      62             : 
      63             :         struct timespec ts;
      64    83818990 :         switch (type) {
      65       29306 :         case core::ClockType::Default: clock_gettime(ClockSource, &ts); break;
      66    83789684 :         case core::ClockType::Monotonic: clock_gettime(CLOCK_MONOTONIC, &ts); break;
      67           0 :         case core::ClockType::Realtime: clock_gettime(CLOCK_REALTIME, &ts); break;
      68           0 :         case core::ClockType::Process: clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); break;
      69           0 :         case core::ClockType::Thread: clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); break;
      70             :         }
      71             : 
      72    83818804 :         return static_cast<uint64_t>(ts.tv_sec) * static_cast<uint64_t>(1000'000) + static_cast<uint64_t>(ts.tv_nsec / 1000);
      73             : }
      74             : 
      75          96 : void sleep(uint64_t microseconds) {
      76          96 :         usleep(useconds_t(microseconds));
      77          96 : }
      78             : 
      79             : }
      80             : 
      81             : #endif

Generated by: LCOV version 1.14