classes.pp 3.5 KB

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