classes.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. {$endif}
  28. {$endif}
  29. { determine the type of the resource/form file }
  30. {$define Win16Res}
  31. {$IFNDEF FPC_DOTTEDUNITS}
  32. unit Classes;
  33. {$ENDIF FPC_DOTTEDUNITS}
  34. {$INLINE ON}
  35. interface
  36. {$IFDEF FPC_DOTTEDUNITS}
  37. uses
  38. System.SysUtils,
  39. System.Types,
  40. System.TypInfo,
  41. {$ifdef FPC_TESTGENERICS}
  42. System.FGL,
  43. {$endif}
  44. System.RtlConsts,
  45. {$ifdef FPC_USE_INTRINSICS}
  46. System.Intrinsics,
  47. {$endif}
  48. System.SortBase;
  49. {$ELSE FPC_DOTTEDUNITS}
  50. uses
  51. sysutils,
  52. types,
  53. typinfo,
  54. {$ifdef FPC_TESTGENERICS}
  55. fgl,
  56. {$endif}
  57. rtlconsts,
  58. {$ifdef FPC_USE_INTRINSICS}
  59. intrinsics,
  60. {$endif}
  61. sortbase;
  62. {$ENDIF FPC_DOTTEDUNITS}
  63. { Also set FPC_USE_INTRINSICS for i386 and x86_64,
  64. but only after _USES clause as there
  65. is not intinsics unit for those CPUs }
  66. {$if defined(CPUI386) or defined(CPUX86_64)}
  67. {$define FPC_USE_INTRINSICS}
  68. {$endif}
  69. {$i classesh.inc}
  70. implementation
  71. {$IFDEF FPC_DOTTEDUNITS}
  72. uses
  73. UnixApi.Base,UnixApi.Unix
  74. ;
  75. {$ELSE FPC_DOTTEDUNITS}
  76. uses
  77. BaseUnix,unix
  78. ;
  79. {$ENDIF FPC_DOTTEDUNITS}
  80. {$IFDEF LINUX}
  81. {$DEFINE HAS_TTHREAD_GETSYSTEMTIMES}
  82. class function TThread.GetSystemTimes(out aSystemTimes : TSystemTimes) : Boolean;
  83. const
  84. StatFile = '/proc/stat';
  85. CPULine = 'cpu';
  86. var
  87. Line: string;
  88. aFile : Text;
  89. Idle : Int64;
  90. Function GetNextWord(var l : String) : String;
  91. var
  92. P : Integer;
  93. begin
  94. P:=Pos(' ',L);
  95. if P=0 then
  96. P:=Length(L)+1;
  97. Result:=Copy(L,1,P-1);
  98. Delete(L,1,P);
  99. L:=Trim(L);
  100. end;
  101. Function GetNextInt : Int64; inline;
  102. begin
  103. Result:=StrToint64(GetNextWord(Line));
  104. end;
  105. begin
  106. Result := False;
  107. aSystemTimes:=Default(TThread.TSystemTimes);
  108. {$i-}
  109. AssignFile(aFile,StatFile);
  110. Reset(aFile);
  111. if IOResult<>0 then
  112. exit;
  113. {$i+}
  114. While not EOF(aFile) do
  115. begin
  116. ReadLn(aFile,Line);
  117. if Pos(CPULine,Line)>0 then
  118. begin
  119. GetNextWord(Line); // Skip "cpu"
  120. // cpuN usertime nicetime kerneltime idletime
  121. With aSystemTimes do
  122. begin
  123. Inc(UserTime, GetNextInt);
  124. Inc(NiceTime, GetNextInt);
  125. Inc(KernelTime, GetNextInt);
  126. Idle:=GetNextInt;
  127. Inc(KernelTime,Idle); // windows seems to count idle as kernel
  128. Inc(IdleTime,Idle);
  129. end;
  130. Result:=True;
  131. end
  132. end;
  133. CloseFile(aFile);
  134. end;
  135. {$ENDIF}
  136. { OS - independent class implementations are in /inc directory. }
  137. {$i classes.inc}
  138. initialization
  139. CommonInit;
  140. finalization
  141. CommonCleanup;
  142. if ThreadsInited then
  143. DoneThreads;
  144. end.