Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(800)

Unified Diff: pkg/analysis_server/lib/src/utilities/profiling.dart

Issue 2927783002: add memory usage to the diagnostics page (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/utilities/profiling.dart
diff --git a/pkg/analysis_server/lib/src/utilities/profiling.dart b/pkg/analysis_server/lib/src/utilities/profiling.dart
new file mode 100644
index 0000000000000000000000000000000000000000..aa3335f6019d96329b771d6b80cdf2740e2f080c
--- /dev/null
+++ b/pkg/analysis_server/lib/src/utilities/profiling.dart
@@ -0,0 +1,85 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+import 'dart:io';
+
+/// A class that can return memory and cpu usage information for a given
+/// process.
+abstract class ProcessProfiler {
+ ProcessProfiler._();
+
+ Future<UsageInfo> getProcessUsage(int processId);
+
+ UsageInfo getProcessUsageSync(int processId);
+
+ /// Return a [ProcessProfiler] instance suitable for the current host
+ /// platform. This can return `null` if we're not able to gather memory and
+ /// cpu information for the current platform.
+ static ProcessProfiler getProfilerForPlatform() {
+ if (Platform.isLinux || Platform.isMacOS) {
+ return new _PosixProcessProfiler();
+ }
+
+ // Not a supported platform.
+ return null;
+ }
+}
+
+class UsageInfo {
+ /// A number between 0.0 and 100.0 * the number of host CPUs (but typically
+ /// never more than slightly above 100.0).
+ final double cpuPercentage;
+
+ /// The process memory usage in bytes.
+ final int memoryKB;
+
+ UsageInfo(this.cpuPercentage, this.memoryKB);
+
+ double get memoryMB => memoryKB / 1024;
+}
+
+class _PosixProcessProfiler extends ProcessProfiler {
+ _PosixProcessProfiler() : super._();
+
+ @override
+ Future<UsageInfo> getProcessUsage(int processId) {
+ try {
+ // Execution time is typically 2-4ms.
+ Future<ProcessResult> future =
+ Process.run('ps', ['-o', '%cpu=,rss=', processId.toString()]);
+ return future.then((ProcessResult result) {
+ if (result.exitCode != 0) {
+ return new Future.value(null);
+ }
+
+ return new Future.value(_parse(result.stdout));
+ });
+ } catch (e) {
+ return new Future.error(e);
+ }
+ }
+
+ UsageInfo getProcessUsageSync(int processId) {
+ try {
+ // Execution time is typically 2-4ms.
+ ProcessResult result =
+ Process.runSync('ps', ['-o', '%cpu=,rss=', processId.toString()]);
+ return result.exitCode == 0 ? _parse(result.stdout) : null;
+ } catch (e) {
+ return null;
+ }
+ }
+
+ UsageInfo _parse(String psResults) {
+ try {
+ // " 0.0 378940"
+ String line = psResults.split('\n').first.trim();
+ List<String> values = line.split(' ');
+ return new UsageInfo(double.parse(values[0]), int.parse(values[1]));
+ } catch (e) {
+ return null;
+ }
+ }
+}
« no previous file with comments | « pkg/analysis_server/lib/src/status/diagnostics.dart ('k') | pkg/analysis_server/test/src/utilities/profiling_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698