Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b46d4e4a51f3514b2fe9591f7339241116adb34c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
 * Copyright (c) 2007 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package com.windriver.tcf.rse.ui;

import java.math.BigInteger;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.rse.core.subsystems.AbstractResource;
import org.eclipse.rse.services.clientserver.processes.IHostProcess;

import com.windriver.tcf.api.protocol.IToken;
import com.windriver.tcf.api.protocol.Protocol;
import com.windriver.tcf.api.services.ISysMonitor;
import com.windriver.tcf.api.services.ISysMonitor.SysMonitorContext;

public class TCFProcessResource extends AbstractResource implements IHostProcess {
    
    private final TCFProcessService rse_service;
    private final ISysMonitor tcf_service;
    private final TCFProcessResource prev;
    private final String id;
    private Throwable error;
    private ISysMonitor.SysMonitorContext context;
    
    private final List<Runnable> children_wait_list = new ArrayList<Runnable>();
    private boolean children_loading;
    private boolean children_loaded;
    private Throwable children_error;
    private boolean running_wait_list;
    
    private long gid = -1;
    private String name;
    private long ppid = -1;
    private long pid = -1;
    private String state;
    private long tgid = -1;
    private long tracepid = -1;
    private long uid;
    private String username;
    private long vm_rss_kb;
    private long vm_size_kb;
    private String utime_pc;
    private String stime_pc;
    private long timestamp;
    
    private Map<String, Object> properties = new HashMap<String, Object>();
    
    private static final NumberFormat percent_format;
    
    static {
        percent_format = NumberFormat.getPercentInstance();
        percent_format.setMaximumFractionDigits(3);
    }
    
    TCFProcessResource(TCFProcessService rse_service, ISysMonitor service,
            TCFProcessResource prev, String id) {
        this.rse_service = rse_service;
        this.tcf_service = service;
        this.prev = prev;
        this.id = id;
    }
    
    public String getID() {
        return id;
    }
    
    public String getParentID() {
        return (String)properties.get(ISysMonitor.PROP_PARENTID);
    }
    
    public TCFProcessService getService() {
        return rse_service;
    }
    
    public void invalidate() {
        assert Protocol.isDispatchThread();
        error = null;
        context = null;
    }
    
    public boolean validate(final Runnable done) {
        assert Protocol.isDispatchThread();
        if (error != null) return true;
        if (context != null) return true;
        tcf_service.getContext(id, new ISysMonitor.DoneGetContext() {

            public void doneGetContext(IToken token, Exception error, SysMonitorContext context) {
                TCFProcessResource.this.error = error;
                TCFProcessResource.this.context = context;
                if (error != null) {
                    properties = new HashMap<String, Object>();
                    gid = -1;
                    name = null;
                    ppid = -1;
                    pid = -1;
                    state = null;
                    tgid = -1;
                    tracepid = -1;
                    uid = 0;
                    username = null;
                    vm_rss_kb = 0;
                    vm_size_kb = 0;
                }
                else {
                    properties = new HashMap<String, Object>(context.getProperties());
                    gid = context.getUGID();
                    name = context.getFile();
                    if (properties.containsKey(ISysMonitor.PROP_PPID)) {
                        ppid = context.getPPID();
                    }
                    pid = context.getPID();
                    state = context.getState();
                    tgid = context.getTGID();
                    if (properties.containsKey(ISysMonitor.PROP_TRACERPID)) {
                        tracepid = context.getTracerPID();
                    }
                    uid = context.getUID();
                    username = context.getUserName();
                    vm_rss_kb = context.getRSS();
                    long page_bytes = context.getPSize();
                    if (page_bytes <= 0 || vm_rss_kb < 0) {
                        vm_rss_kb = -1;
                    }
                    else {
                        vm_rss_kb = (vm_rss_kb * page_bytes + 1023) / 1024;
                    }
                    vm_size_kb = (context.getVSize() + 1023) / 1024;
                }
                timestamp = System.currentTimeMillis();
                Protocol.invokeLater(done);
            }
            
        });
        return false;
    }
    
    public Throwable getError() {
        assert Protocol.isDispatchThread();
        return error;
    }
    
    public ISysMonitor.SysMonitorContext getContext() {
        assert Protocol.isDispatchThread();
        return context;
    }
    
    // IHostProcess methods

    public String getAllProperties() {
        // TODO Auto-generated method stub
        return null;
    }

    public long getGid() {
        return gid;
    }

    public String getLabel() {
        return Long.toString(getPid()) + " " + name;
    }

    public String getName() {
        return name;
    }

    public long getPPid() {
        return ppid;
    }

    public long getPid() {
        return pid;
    }

    public String getState() {
        return state;
    }

    public long getTgid() {
        return tgid;
    }

    public long getTracerPid() {
        return tracepid;
    }

    public long getUid() {
        return uid;
    }

    public String getUsername() {
        return username;
    }

    public long getVmRSSInKB() {
        return vm_rss_kb;
    }

    public long getVmSizeInKB() {
        return vm_size_kb;
    }

    public boolean isRoot() {
        return true;
    }
    
    String getStatusLine() {
        final String STATUS_DELIMITER = "|"; //$NON-NLS-1$
        StringBuffer s = new StringBuffer();
        s.append(getPid()).append(STATUS_DELIMITER);
        s.append(name).append(STATUS_DELIMITER);
        s.append(getState()).append(STATUS_DELIMITER);
        s.append(getTgid()).append(STATUS_DELIMITER);
        s.append(getPPid()).append(STATUS_DELIMITER);
        s.append('0').append(STATUS_DELIMITER);
        s.append(getUid()).append(STATUS_DELIMITER);
        s.append(getUsername()).append(STATUS_DELIMITER);
        s.append(getGid()).append(STATUS_DELIMITER);
        s.append(getVmSizeInKB()).append(STATUS_DELIMITER);
        s.append(getVmRSSInKB()).append(STATUS_DELIMITER);
        return s.toString();
    }

    public Map<String,Object> getProperties() {
        return properties;
    }
    
    private String getTimePC(String name) {
        if (prev == null) return null;
        Object x = prev.properties.get(name);
        Object y = properties.get(name);
        if (x instanceof Number && y instanceof Number) {
            BigInteger nx = x instanceof BigInteger ? (BigInteger)x : new BigInteger(x.toString());
            BigInteger ny = y instanceof BigInteger ? (BigInteger)y : new BigInteger(y.toString());
            double d = ny.subtract(nx).doubleValue() / (timestamp - prev.timestamp);
            return percent_format.format(d);
        }
        return null;
    }
    
    public String getUserTimePC() {
        if (utime_pc != null) return utime_pc;
        return utime_pc = getTimePC(ISysMonitor.PROP_UTIME);
    }

    public String getSysTimePC() {
        if (stime_pc != null) return stime_pc;
        return stime_pc = getTimePC(ISysMonitor.PROP_STIME);
    }
    
    public boolean getChildrenLoading() {
        return children_loading;
    }
    
    public void setChildrenLoading(boolean b) {
        children_loading = b;
    }
    
    public boolean getChildrenLoaded() {
        return children_loaded;
    }
    
    public void setChildrenLoaded(boolean b) {
        children_loaded = b;
    }
    
    public Throwable getChildrenError() {
        return children_error;
    }
    
    public void setChildrenError(Throwable error) {
        children_error = error;
    }
    
    public void addChildrenWaitList(Runnable run) {
        assert !running_wait_list;
        assert children_loading;
        assert !children_loaded;
        children_wait_list.add(run);
    }
    
    public void runChildrenWaitList() {
        assert !children_loading;
        assert children_loaded;
        try {
            running_wait_list = true;
            for (Runnable r : children_wait_list) r.run();
            children_wait_list.clear();
        }
        finally {
            running_wait_list = false;
        }
    }
    
    public void cancelChildrenLoading() {
        // TODO: cancelChildrenLoading
    }
    
    public String toString() {
        return "[" + getStatusLine() + "]";
    }
}

Back to the top