classes.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  4. Classes unit for linux
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. {$modeswitch advancedrecords}
  14. {$if FPC_FULLVERSION>=30301}
  15. {$modeswitch FUNCTIONREFERENCES}
  16. {$define FPC_HAS_REFERENCE_PROCEDURE}
  17. {$ifndef CPULLVM}
  18. {$if DEFINED(CPUARM) or DEFINED(CPUAARCH64)}
  19. {$define FPC_USE_INTRINSICS}
  20. {$endif}
  21. {$if defined(CPUPOWERPC) or defined(CPUPOWERPC64)}
  22. {$define FPC_USE_INTRINSICS}
  23. {$endif}
  24. {$if defined(CPURISCV32) or defined(CPURISCV64)}
  25. {$define FPC_USE_INTRINSICS}
  26. {$endif}
  27. { Also set FPC_USE_INTRINSICS for i386 and x86_64 }
  28. {$if defined(CPURISCV32) or defined(CPURISCV64)}
  29. {$define FPC_USE_INTRINSICS}
  30. {$endif}
  31. {$endif}
  32. {$endif}
  33. { determine the type of the resource/form file }
  34. {$define Win16Res}
  35. {$IFNDEF FPC_DOTTEDUNITS}
  36. unit Classes;
  37. {$ENDIF FPC_DOTTEDUNITS}
  38. {$INLINE ON}
  39. interface
  40. {$IFDEF FPC_DOTTEDUNITS}
  41. uses
  42. System.SysUtils,
  43. System.Types,
  44. System.TypInfo,
  45. {$ifdef FPC_TESTGENERICS}
  46. System.FGL,
  47. {$endif}
  48. System.RtlConsts,
  49. {$ifdef FPC_USE_INTRINSICS}
  50. System.Intrinsics,
  51. {$endif}
  52. System.SortBase;
  53. {$ELSE FPC_DOTTEDUNITS}
  54. uses
  55. sysutils,
  56. types,
  57. typinfo,
  58. {$ifdef FPC_TESTGENERICS}
  59. fgl,
  60. {$endif}
  61. rtlconsts,
  62. {$ifdef FPC_USE_INTRINSICS}
  63. intrinsics,
  64. {$endif}
  65. sortbase;
  66. {$ENDIF FPC_DOTTEDUNITS}
  67. {$i classesh.inc}
  68. implementation
  69. {$IFDEF FPC_DOTTEDUNITS}
  70. uses
  71. UnixApi.Base,UnixApi.Unix
  72. ;
  73. {$ELSE FPC_DOTTEDUNITS}
  74. uses
  75. BaseUnix,unix
  76. ;
  77. {$ENDIF FPC_DOTTEDUNITS}
  78. {$IFDEF LINUX}
  79. {$DEFINE HAS_TTHREAD_GETSYSTEMTIMES}
  80. class function TThread.GetSystemTimes(out aSystemTimes : TSystemTimes) : Boolean;
  81. const
  82. StatFile = '/proc/stat';
  83. CPULine = 'cpu';
  84. var
  85. Line: string;
  86. aFile : Text;
  87. Idle : Int64;
  88. Function GetNextWord(var l : String) : String;
  89. var
  90. P : Integer;
  91. begin
  92. P:=Pos(' ',L);
  93. if P=0 then
  94. P:=Length(L)+1;
  95. Result:=Copy(L,1,P-1);
  96. Delete(L,1,P);
  97. L:=Trim(L);
  98. end;
  99. Function GetNextInt : Int64; inline;
  100. begin
  101. Result:=StrToint64(GetNextWord(Line));
  102. end;
  103. begin
  104. Result := False;
  105. aSystemTimes:=Default(TThread.TSystemTimes);
  106. {$i-}
  107. AssignFile(aFile,StatFile);
  108. Reset(aFile);
  109. if IOResult<>0 then
  110. exit;
  111. {$i+}
  112. While not EOF(aFile) do
  113. begin
  114. ReadLn(aFile,Line);
  115. if Pos(CPULine,Line)>0 then
  116. begin
  117. GetNextWord(Line); // Skip "cpu"
  118. // cpuN usertime nicetime kerneltime idletime
  119. With aSystemTimes do
  120. begin
  121. Inc(UserTime, GetNextInt);
  122. Inc(NiceTime, GetNextInt);
  123. Inc(KernelTime, GetNextInt);
  124. Idle:=GetNextInt;
  125. Inc(KernelTime,Idle); // windows seems to count idle as kernel
  126. Inc(IdleTime,Idle);
  127. end;
  128. Result:=True;
  129. end
  130. end;
  131. CloseFile(aFile);
  132. end;
  133. {$ENDIF}
  134. { OS - independent class implementations are in /inc directory. }
  135. {$i classes.inc}
  136. initialization
  137. CommonInit;
  138. finalization
  139. CommonCleanup;
  140. if ThreadsInited then
  141. DoneThreads;
  142. end.