classes.pp 3.1 KB

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