aboutsummaryrefslogtreecommitdiffstats
blob: a61aa621163a1e7c41bc79601e981eb068937ac9 (plain)
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
/********************************************************************************
 * Copyright (c) 2008 Motorola Inc. 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
 *
 * Initial Contributor:
 * Otávio Ferranti (Motorola)
 *
 * Contributors:
 * {Name} (company) - description of contribution.
 ********************************************************************************/

package org.eclipse.tml.linuxtools.tools.cpuload;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.MatchResult;

import org.eclipse.tml.common.utilities.logger.ILogger;

import org.eclipse.tml.linuxtools.LinuxToolsPlugin;
import org.eclipse.tml.linuxtools.network.IConstants.CommandCode;
import org.eclipse.tml.linuxtools.network.IConstants.EventCode;
import org.eclipse.tml.linuxtools.network.IConnectionProvider;
import org.eclipse.tml.linuxtools.tools.AbstractNotifier;
import org.eclipse.tml.linuxtools.tools.IListener;
import org.eclipse.tml.linuxtools.tools.INotifier;

/**
 * @author Otávio Ferranti
 */
public class CpuLoadProcessor extends AbstractNotifier implements IListener {

	final private String CMD_FETCH_STAT = "/proc/stat"; //$NON-NLS-1$
	final private String PARSE_PATTERN_1 = "cpu(\\d*)(.*)"; //$NON-NLS-1$;
	final private String PARSE_PATTERN_2 = "\\s+(\\d+)"; //$NON-NLS-1$;
	final private String DATA_FORMAT = "%1$02.1f %%"; //$NON-NLS-1$;
	
	final private String MSG_EXECUTING_COMMAND =
		Messages.CpuLoadProcessor_Msg_Executing_the_command;

	final private String MSG_GOT_RESULT =
		Messages.CpuLoadProcessor_Msg_Got_The_Result;
	
	final private int MAX_COLUMNS = 100;
	
	private int[][] previousData = null;
	private long[] previousTotal = null;
	private IConnectionProvider connectionProvider = null;
	
	private ILogger logger = null;
	
	/**
	 * The constructor;
	 * @param connectionProvider
	 */
	public CpuLoadProcessor(IConnectionProvider connectionProvider) {
		setConnectionProvider(connectionProvider);
		logger = LinuxToolsPlugin.getLogger();
	}
	
	/**
	 * Requests data.
	 * @throws IOException
	 */
	public void gatherData() throws IOException {
		this.connectionProvider.sendCommand(CommandCode.FETCH_FILE, this.CMD_FETCH_STAT);
		logger.debug(MSG_EXECUTING_COMMAND + "\n" + this.CMD_FETCH_STAT); //$NON-NLS-1$
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.tml.linuxtools.network.IListener#notify(org.eclipse.tml.linuxtools.network.INotifier, org.eclipse.tml.linuxtools.network.IConstants.EventCode, java.lang.Object)
	 */
	public void notify(INotifier notifier,
						EventCode event,
						Object result) {
		if (notifier == this.connectionProvider &&
				event == EventCode.EVT_PROVIDER_SENDCOMMAND_FINISHED) {
			Object[][] parsedResult = parseStat((StringBuffer) result);
			this.notifyListeners(EventCode.EVT_PROCESSOR_GATHERDATA_FINISHED,
					parsedResult);
		}
	}
	
	/**
	 * Set the connection provider.
	 * @param connectionProvider
	 */
	public void setConnectionProvider (IConnectionProvider connectionProvider) {
		if (null != this.connectionProvider) {
			this.connectionProvider.removeListener(this);
		}
		this.connectionProvider = connectionProvider;
		if (null != this.connectionProvider) {
			this.connectionProvider.addListener(this);
		}
	}

	/**
	 * @param currentDataStr
	 * @return
	 */
	private Object[][] calculateTimes(String[][] currentDataStr) {
		String[][] resultStr = null;
		
		if (null == currentDataStr) {
			currentDataStr = new String[][] {{"0"}};
		}
		if (null == previousData) {
			previousData = new int[][] {{0}};
			previousTotal = new long[] {1};
		}
		if (previousData.length != currentDataStr.length ||
			previousData[0].length != currentDataStr[0].length) {
			previousData = String2int(currentDataStr);
			previousTotal = new long[previousData.length];
			
			for (int j = 0; j < previousData.length; j++) {
				long total = 0;
				for (int i = 0; i < previousData[0].length; i++) {
					total += previousData[j][i];
				}
				previousTotal[j] = total;
			}
		}
		final int[][] currentDataInt = String2int(currentDataStr);
		float[][] resultFloat = new float[currentDataStr.length][currentDataStr[0].length];

		for (int j = 0; j < currentDataInt.length; j++) {
			long total = 0;
			for (int i = 0; i < currentDataInt[0].length; i++) {
				total += currentDataInt[j][i];
			}
			float deltaTotal = total - previousTotal[j];
			previousTotal[j] = total;
			for (int i = 0; i < currentDataInt[0].length; i++) {
				float deltaData = currentDataInt[j][i] - previousData[j][i];
				if (0 == deltaTotal) {
					resultFloat[j][i] = 0;
				} else {
					resultFloat[j][i] = 100* deltaData / deltaTotal;
				}
			}
		}
		previousData = currentDataInt;
		resultStr = float2StringFormated(resultFloat, DATA_FORMAT);
		return resultStr;
	}

	/**
	 * @param target
	 * @param Source
	 */
	private void copyData(Object[][] target, Object[][] Source) {
		for (int j = 0; j < target.length; j++) {
			for (int i = 1; i < target[0].length; i++) {
				target[j][i] = Source[j][i-1];
			}
		}		
	}
	
	/**
	 * @param input
	 * @return
	 */
	private String[][] float2StringFormated (float[][] input, String format) {
		String[][] output = null;
		if (null != input) {
			output = new String[input.length][input[0].length];
			for (int j = 0; j < output.length; j++) {
				for (int i = 0; i < output[0].length; i++) {
					output[j][i] = String.format(format, new Float(input[j][i]));
				}
			}
		}
		return output;
	}

	/**
	 * @param data
	 * @return
	 */
	private Object[][] parseStat(StringBuffer data) {
		logger.debug(MSG_GOT_RESULT + "\n" + data.toString());

		int requiredColumns = 0;
		
		Scanner s1 = new Scanner(data.toString());
		ArrayList<String[]> list = new ArrayList<String[]>();
		
		while (s1.hasNextLine()) {
			String[] entry = new String[MAX_COLUMNS];;
			
			Scanner s2 = new Scanner(s1.nextLine());
			s2.findInLine(PARSE_PATTERN_1);
			
			try {

				MatchResult result = s2.match();
				entry[0] = result.group(1).trim();
		    
				String aux = result.group(2);
				
		    	Scanner s3 = new Scanner(aux);
		    	entry[1] = s3.findInLine(PARSE_PATTERN_2).trim();
		    	
		    	s3.match();
		    	int i = 2;
		    	
		    	while (s3.hasNext() && i < MAX_COLUMNS) {
		    		entry[i] = s3.next().trim();

		    		if (i > requiredColumns) {
			    		requiredColumns = i;
			    	}
			    	i++;
		    	}
				s3.close();
			    if (null != entry) {
			    	list.add(entry);
			    }
			} catch (IllegalStateException ise) {
			}
		    s2.close();
		}
	    s1.close();
	    
	    String[][] dataAux = new String[list.size()][requiredColumns + 1];
	    for (int j = 0; j < dataAux.length; j++) {
		    for (int i = 0; i < dataAux[0].length; i++) {
		    	dataAux[j][i] = list.get(j)[i];
		    }
	    }
	    String[][] dataStrippedAux = stripFirstColumn(dataAux);
	    Object[][] result = calculateTimes(dataStrippedAux);
	    copyData(dataAux, result);
		return dataAux;
	}

	/**
	 * @param input
	 * @return
	 */
	private int[][] String2int (String[][] input) {
		int[][] output = null;
		if (null != input) {
			output = new int[input.length][input[0].length];
			for (int j = 0; j < output.length; j++) {
				for (int i = 0; i < output[0].length; i++) {
					try {
						output[j][i] = new Integer(input[j][i]).intValue();
					}
					catch (NumberFormatException nfe) {
						output[j][i] = -1;
					}
				}
			}
		}
		return output;
	}
	
	/**
	 * @param input
	 * @return
	 */
	private String[][] stripFirstColumn (String[][] input) {
		String output[][] = null;
		if (null != input) {
			output = new String[input.length][input[0].length-1];
			for (int j = 0; j < output.length; j++) {
				for (int i = 0; i < output[0].length; i++) {
					output[j][i] = input[j][i+1];
				}
			}
		}
		return output;
	}
}