OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 /// A class that can return memory and cpu usage information for a given | 8 /// A class that can return memory and cpu usage information for a given |
9 /// process. | 9 /// process. |
10 abstract class ProcessProfiler { | 10 abstract class ProcessProfiler { |
(...skipping 14 matching lines...) Expand all Loading... |
25 // Not a supported platform. | 25 // Not a supported platform. |
26 return null; | 26 return null; |
27 } | 27 } |
28 } | 28 } |
29 | 29 |
30 class UsageInfo { | 30 class UsageInfo { |
31 /// A number between 0.0 and 100.0 * the number of host CPUs (but typically | 31 /// A number between 0.0 and 100.0 * the number of host CPUs (but typically |
32 /// never more than slightly above 100.0). | 32 /// never more than slightly above 100.0). |
33 final double cpuPercentage; | 33 final double cpuPercentage; |
34 | 34 |
35 /// The process memory usage in bytes. | 35 /// The process memory usage in kilobytes. |
36 final int memoryKB; | 36 final int memoryKB; |
37 | 37 |
38 UsageInfo(this.cpuPercentage, this.memoryKB); | 38 UsageInfo(this.cpuPercentage, this.memoryKB); |
39 | 39 |
40 double get memoryMB => memoryKB / 1024; | 40 double get memoryMB => memoryKB / 1024; |
| 41 |
| 42 String toString() => '$cpuPercentage% ${memoryMB.toStringAsFixed(1)}MB'; |
41 } | 43 } |
42 | 44 |
43 class _PosixProcessProfiler extends ProcessProfiler { | 45 class _PosixProcessProfiler extends ProcessProfiler { |
| 46 static final RegExp stringSplitRegExp = new RegExp(r'\s+'); |
| 47 |
44 _PosixProcessProfiler() : super._(); | 48 _PosixProcessProfiler() : super._(); |
45 | 49 |
46 @override | 50 @override |
47 Future<UsageInfo> getProcessUsage(int processId) { | 51 Future<UsageInfo> getProcessUsage(int processId) { |
48 try { | 52 try { |
49 // Execution time is typically 2-4ms. | 53 // Execution time is typically 2-4ms. |
50 Future<ProcessResult> future = | 54 Future<ProcessResult> future = |
51 Process.run('ps', ['-o', '%cpu=,rss=', processId.toString()]); | 55 Process.run('ps', ['-o', '%cpu=,rss=', processId.toString()]); |
52 return future.then((ProcessResult result) { | 56 return future.then((ProcessResult result) { |
53 if (result.exitCode != 0) { | 57 if (result.exitCode != 0) { |
(...skipping 15 matching lines...) Expand all Loading... |
69 return result.exitCode == 0 ? _parse(result.stdout) : null; | 73 return result.exitCode == 0 ? _parse(result.stdout) : null; |
70 } catch (e) { | 74 } catch (e) { |
71 return null; | 75 return null; |
72 } | 76 } |
73 } | 77 } |
74 | 78 |
75 UsageInfo _parse(String psResults) { | 79 UsageInfo _parse(String psResults) { |
76 try { | 80 try { |
77 // " 0.0 378940" | 81 // " 0.0 378940" |
78 String line = psResults.split('\n').first.trim(); | 82 String line = psResults.split('\n').first.trim(); |
79 List<String> values = line.split(' '); | 83 List<String> values = line.split(stringSplitRegExp); |
80 return new UsageInfo(double.parse(values[0]), int.parse(values[1])); | 84 return new UsageInfo(double.parse(values[0]), int.parse(values[1])); |
81 } catch (e) { | 85 } catch (e) { |
82 return null; | 86 return null; |
83 } | 87 } |
84 } | 88 } |
85 } | 89 } |
OLD | NEW |