OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 abstract class _IOResourceInfo { | 7 abstract class _IOResourceInfo { |
8 final String type; | 8 final String type; |
9 final int id; | 9 final int id; |
10 String get name; | 10 String get name; |
11 static int _count = 0; | 11 static int _count = 0; |
12 | 12 |
| 13 static final Stopwatch _sw = new Stopwatch()..start(); |
| 14 |
13 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); | 15 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); |
14 | 16 |
15 String toJSON(); | |
16 | |
17 /// Get the full set of values for a specific implementation. This is normally | 17 /// Get the full set of values for a specific implementation. This is normally |
18 /// looked up based on an id from a referenceValueMap. | 18 /// looked up based on an id from a referenceValueMap. |
19 Map<String, String> get fullValueMap; | 19 Map<String, String> get fullValueMap; |
20 | 20 |
21 /// The reference map, used to return a list of values, e.g., getting | 21 /// The reference map, used to return a list of values, e.g., getting |
22 /// all open sockets. The structure of this is shared among all subclasses. | 22 /// all open sockets. The structure of this is shared among all subclasses. |
23 Map<String, String> get referenceValueMap => | 23 Map<String, String> get referenceValueMap => |
24 { | 24 { |
25 // The type for a reference object is prefixed with @ in observatory. | 25 // The type for a reference object is prefixed with @ in observatory. |
26 'type': '@$type', | 26 'type': '@$type', |
27 'id': id, | 27 'id': id, |
28 'name': name, | 28 'name': name, |
29 }; | 29 }; |
30 | 30 |
31 static int getNextID() => _count++; | 31 static int getNextID() => _count++; |
32 } | 32 } |
33 | 33 |
34 abstract class _ReadWriteResourceInfo extends _IOResourceInfo { | 34 abstract class _ReadWriteResourceInfo extends _IOResourceInfo { |
35 int totalRead; | 35 int totalRead; |
36 int totalWritten; | 36 int totalWritten; |
37 int readCount; | 37 int readCount; |
38 int writeCount; | 38 int writeCount; |
39 double lastRead; | 39 double lastRead; |
40 double lastWrite; | 40 double lastWrite; |
41 | 41 |
42 static final Stopwatch _sw = new Stopwatch()..start(); | 42 static double get timestamp => |
43 static double get timestamp => _sw.elapsedMicroseconds / 1000000.0; | 43 _IOResourceInfo._sw.elapsedMicroseconds / 1000000.0; |
44 | 44 |
45 // Not all call sites use this. In some cases, e.g., a socket, a read does | 45 // Not all call sites use this. In some cases, e.g., a socket, a read does |
46 // not always mean that we actually read some bytes (we may do a read to see | 46 // not always mean that we actually read some bytes (we may do a read to see |
47 // if there are some bytes available). | 47 // if there are some bytes available). |
48 void addRead(int bytes) { | 48 void addRead(int bytes) { |
49 totalRead += bytes; | 49 totalRead += bytes; |
50 readCount++; | 50 readCount++; |
51 lastRead = timestamp; | 51 lastRead = timestamp; |
52 } | 52 } |
53 | 53 |
(...skipping 22 matching lines...) Expand all Loading... |
76 'type': type, | 76 'type': type, |
77 'id': id, | 77 'id': id, |
78 'name': name, | 78 'name': name, |
79 'total_read': totalRead, | 79 'total_read': totalRead, |
80 'total_written': totalWritten, | 80 'total_written': totalWritten, |
81 'read_count': readCount, | 81 'read_count': readCount, |
82 'write_count': writeCount, | 82 'write_count': writeCount, |
83 'last_read': lastRead, | 83 'last_read': lastRead, |
84 'last_write': lastWrite | 84 'last_write': lastWrite |
85 }; | 85 }; |
86 | |
87 String toJSON() { | |
88 return JSON.encode(fullValueMap); | |
89 } | |
90 } | 86 } |
91 | 87 |
92 class _FileResourceInfo extends _ReadWriteResourceInfo { | 88 class _FileResourceInfo extends _ReadWriteResourceInfo { |
93 static const String TYPE = '_file'; | 89 static const String TYPE = '_file'; |
94 | 90 |
95 final file; | 91 final file; |
96 | 92 |
97 static Map<int, _FileResourceInfo> openFiles = | 93 static Map<int, _FileResourceInfo> openFiles = |
98 new Map<int, _FileResourceInfo>(); | 94 new Map<int, _FileResourceInfo>(); |
99 | 95 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 openFiles.containsKey(id) ? openFiles[id].getFileInfoMap() : {}; | 130 openFiles.containsKey(id) ? openFiles[id].getFileInfoMap() : {}; |
135 var json = JSON.encode(result); | 131 var json = JSON.encode(result); |
136 return new Future.value(new ServiceExtensionResponse.result(json)); | 132 return new Future.value(new ServiceExtensionResponse.result(json)); |
137 } | 133 } |
138 | 134 |
139 String get name { | 135 String get name { |
140 return '${file.path}'; | 136 return '${file.path}'; |
141 } | 137 } |
142 } | 138 } |
143 | 139 |
| 140 class _ProcessResourceInfo extends _IOResourceInfo{ |
| 141 static const String TYPE = '_process'; |
| 142 final process; |
| 143 final int startedAt; |
| 144 |
| 145 static Map<int, _ProcessResourceInfo> startedProcesses = |
| 146 new Map<int, _ProcessResourceInfo>(); |
| 147 |
| 148 _ProcessResourceInfo(this.process) : |
| 149 startedAt = new DateTime.now().millisecondsSinceEpoch, |
| 150 super(TYPE) { |
| 151 ProcessStarted(this); |
| 152 } |
| 153 |
| 154 String get name => process._path; |
| 155 |
| 156 void stopped() => ProcessStopped(this); |
| 157 |
| 158 Map<String, String> get fullValueMap => |
| 159 { |
| 160 'type': type, |
| 161 'id': id, |
| 162 'name': name, |
| 163 'pid': process.pid, |
| 164 'started_at': startedAt, |
| 165 'arguments': process._arguments, |
| 166 'working_directory': |
| 167 process._workingDirectory == null ? '.' : process._workingDirectory, |
| 168 }; |
| 169 |
| 170 static ProcessStarted(_ProcessResourceInfo info) { |
| 171 assert(!startedProcesses.containsKey(info.id)); |
| 172 startedProcesses[info.id] = info; |
| 173 } |
| 174 |
| 175 static ProcessStopped(_ProcessResourceInfo info) { |
| 176 assert(startedProcesses.containsKey(info.id)); |
| 177 startedProcesses.remove(info.id); |
| 178 } |
| 179 |
| 180 static Iterable<Map<String, String>> getStartedProcessesList() => |
| 181 new List.from(startedProcesses.values.map((e) => e.referenceValueMap)); |
| 182 |
| 183 static Future<ServiceExtensionResponse> getStartedProcesses( |
| 184 String function, Map<String, String> params) { |
| 185 assert(function == '__getProcesses'); |
| 186 var data = {'type': '_startedprocesses', 'data': getStartedProcessesList()}; |
| 187 var json = JSON.encode(data); |
| 188 return new Future.value(new ServiceExtensionResponse.result(json)); |
| 189 } |
| 190 |
| 191 static Future<ServiceExtensionResponse> getProcessInfoMapById( |
| 192 String function, Map<String, String> params) { |
| 193 var id = int.parse(params['id']); |
| 194 var result = startedProcesses.containsKey(id) |
| 195 ? startedProcesses[id].fullValueMap |
| 196 : {}; |
| 197 var json = JSON.encode(result); |
| 198 return new Future.value(new ServiceExtensionResponse.result(json)); |
| 199 } |
| 200 } |
| 201 |
144 class _SocketResourceInfo extends _ReadWriteResourceInfo { | 202 class _SocketResourceInfo extends _ReadWriteResourceInfo { |
145 static const String TCP_STRING = 'TCP'; | 203 static const String TCP_STRING = 'TCP'; |
146 static const String UDP_STRING = 'UDP'; | 204 static const String UDP_STRING = 'UDP'; |
147 static const String TYPE = '_socket'; | 205 static const String TYPE = '_socket'; |
148 | 206 |
149 final socket; | 207 final socket; |
150 | 208 |
151 static Map<int, _SocketResourceInfo> openSockets = | 209 static Map<int, _SocketResourceInfo> openSockets = |
152 new Map<int, _SocketResourceInfo>(); | 210 new Map<int, _SocketResourceInfo>(); |
153 | 211 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 assert(!openSockets.containsKey(info.id)); | 274 assert(!openSockets.containsKey(info.id)); |
217 openSockets[info.id] = info; | 275 openSockets[info.id] = info; |
218 } | 276 } |
219 | 277 |
220 static SocketClosed(_SocketResourceInfo info) { | 278 static SocketClosed(_SocketResourceInfo info) { |
221 assert(openSockets.containsKey(info.id)); | 279 assert(openSockets.containsKey(info.id)); |
222 openSockets.remove(info.id); | 280 openSockets.remove(info.id); |
223 } | 281 } |
224 | 282 |
225 } | 283 } |
OLD | NEW |