classes.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 FPC_FULLVERSION>=30301}
  67. {$ifndef CPULLVM}
  68. {$if defined(CPUI386) or defined(CPUX86_64)}
  69. {$define FPC_USE_INTRINSICS}
  70. {$endif}
  71. {$endif}
  72. {$endif}
  73. {$i classesh.inc}
  74. implementation
  75. {$IFDEF FPC_DOTTEDUNITS}
  76. uses
  77. UnixApi.Base,UnixApi.Unix
  78. ;
  79. {$ELSE FPC_DOTTEDUNITS}
  80. uses
  81. BaseUnix,unix
  82. ;
  83. {$ENDIF FPC_DOTTEDUNITS}
  84. {$IFDEF LINUX}
  85. {$DEFINE HAS_TTHREAD_GETSYSTEMTIMES}
  86. class function TThread.GetSystemTimes(out aSystemTimes : TSystemTimes) : Boolean;
  87. const
  88. StatFile = '/proc/stat';
  89. CPULine = 'cpu';
  90. var
  91. Line: string;
  92. aFile : Text;
  93. Idle : Int64;
  94. Function GetNextWord(var l : String) : String;
  95. var
  96. P : Integer;
  97. begin
  98. P:=Pos(' ',L);
  99. if P=0 then
  100. P:=Length(L)+1;
  101. Result:=Copy(L,1,P-1);
  102. Delete(L,1,P);
  103. L:=Trim(L);
  104. end;
  105. Function GetNextInt : Int64; inline;
  106. begin
  107. Result:=StrToint64(GetNextWord(Line));
  108. end;
  109. begin
  110. Result := False;
  111. aSystemTimes:=Default(TThread.TSystemTimes);
  112. {$i-}
  113. AssignFile(aFile,StatFile);
  114. Reset(aFile);
  115. if IOResult<>0 then
  116. exit;
  117. {$i+}
  118. While not EOF(aFile) do
  119. begin
  120. ReadLn(aFile,Line);
  121. if Pos(CPULine,Line)>0 then
  122. begin
  123. GetNextWord(Line); // Skip "cpu"
  124. // cpuN usertime nicetime kerneltime idletime
  125. With aSystemTimes do
  126. begin
  127. Inc(UserTime, GetNextInt);
  128. Inc(NiceTime, GetNextInt);
  129. Inc(KernelTime, GetNextInt);
  130. Idle:=GetNextInt;
  131. Inc(KernelTime,Idle); // windows seems to count idle as kernel
  132. Inc(IdleTime,Idle);
  133. end;
  134. Result:=True;
  135. end
  136. end;
  137. CloseFile(aFile);
  138. end;
  139. {$ENDIF}
  140. { OS - independent class implementations are in /inc directory. }
  141. {$i classes.inc}
  142. initialization
  143. CommonInit;
  144. finalization
  145. CommonCleanup;
  146. if ThreadsInited then
  147. DoneThreads;
  148. end.