Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dfc9b87d09592b04887fd253ea35efa5e2e6bf2c (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 Tilera Corporation 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:
 *     William R. Swanson (Tilera Corporation) - initial API and implementation
 *     Marc Dumais (Ericsson) - Bug 405390
 *******************************************************************************/

package org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.ui.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.eclipse.cdt.visualizer.ui.util.Todo;

/** 
 * Class representing the state of the data to display in the MulticoreVisualizer. 
 */
public class VisualizerModel
{
	// --- members ---
	
	/** List of cpus (and cores) */
	protected ArrayList<VisualizerCPU> m_cpus;
	
	/** Lookup table for CPUs */
	protected Hashtable<Integer, VisualizerCPU> m_cpuMap;
	
	/** List of threads */
	protected ArrayList<VisualizerThread> m_threads;
	
	/** Completion state tracker. */
	protected Todo m_todo;
	
	/** Completion state tracker for load meters. */
	protected Todo m_loadTodo;
	
	// Setting to remove exited threads, or keep them shown.
	// If we are to support this, we should have a preference
	// and a way to for the user to clean up old threads,
	// or maybe a timeout to remove them.
	private boolean m_keepExitedThreads = false;
	
	protected boolean m_loadMetersEnabled = false;
	
	// --- constructors/destructors ---
	
	/** Constructor */
	public VisualizerModel() {
		m_cpus = new ArrayList<VisualizerCPU>();
		m_cpuMap = new Hashtable<Integer, VisualizerCPU>();
		m_threads = new ArrayList<VisualizerThread>();
		m_todo = new Todo();
		m_loadTodo = new Todo();
	}
	
	/** Dispose method */
	public void dispose() {
		if (m_cpus != null) {
			for (VisualizerCPU cpu : m_cpus) {
				cpu.dispose();
			}
			m_cpuMap.clear();
			m_cpuMap = null;
			m_cpus.clear();
			m_cpus = null;
		}
		if (m_threads != null) {
			for (VisualizerThread thread : m_threads) {
				thread.dispose();
			}
			m_threads.clear();
			m_threads = null;
		}
		if (m_todo != null) {
			m_todo.dispose();
			m_todo = null;
		}
		if (m_loadTodo != null) {
			m_loadTodo.dispose();
			m_loadTodo = null;
		}
	}
	
	
	// --- accessors ---
	
	/** Gets completion state tracker. */
	public Todo getTodo() {
		return m_todo;
	}
	
	/** Gets completion state tracker. */
	public Todo getLoadTodo() {
		return m_loadTodo;
	}
	
	public void setLoadMetersEnabled (boolean enable) {
		m_loadMetersEnabled = enable;
	}
	
	public boolean getLoadMetersEnabled () {
		return m_loadMetersEnabled;
	}
	
	// --- methods ---
	
	/** Sorts cores, cpus, etc. by IDs. */
	public void sort() {
		Collections.sort(m_cpus);
		for (VisualizerCPU cpu : m_cpus) cpu.sort();
		Collections.sort(m_threads);
	}
	
	
	// --- core/cpu management ---
	
	/** Gets number of CPUs. */
	public int getCPUCount() {
		return m_cpus.size();
	}
	
	/** Gets number of cores. */
	public int getCoreCount() {
		int count = 0;
		
		for(VisualizerCPU cpu : m_cpus) {
			count += cpu.getCoreCount();
		}
		return count;
	}
	
	/** Gets number of threads. */
	public int getThreadCount () {
		return m_threads.size();
	}
	
	/** Gets CPU with specified ID. */
	public VisualizerCPU getCPU(int id) {
		return m_cpuMap.get(id);
	}
	
	/** Gets Core with specified ID. */
	public VisualizerCore getCore(int id) {
		VisualizerCore result = null;
		for (VisualizerCPU cpu: m_cpus) {
			result = cpu.getCore(id);
			if (result != null) break;
		}
		return result;
	}
	
	/** Gets CPU set. */
	public List<VisualizerCPU> getCPUs() {
		return m_cpus;
	}
	
	/** Adds CPU. */
	public VisualizerCPU addCPU(VisualizerCPU cpu) {
		m_cpus.add(cpu);
		m_cpuMap.put(cpu.getID(), cpu);
		return cpu;
	}

	/** Removes CPU. */
	public void removeCPU(VisualizerCPU cpu) {
		m_cpus.remove(cpu);
		m_cpuMap.remove(cpu.getID());
	}

	
	/** Gets maximum number of cores per CPU. */
	public int getCoresPerCPU() {
		int maxCores = 1;
		for (VisualizerCPU cpu : m_cpus) {
			int cores = cpu.getCoreCount();
			if (cores > maxCores) maxCores = cores;
		}
		return maxCores;
	}
	
	
	// --- thread management ---
	
	/** Gets threads. */
	public List<VisualizerThread> getThreads() {
		return m_threads;
	}

	/** 
	 * Finds thread(s) by process ID.
	 * If no threads are found, returns null rather
	 * than an empty list.
	 */
	public List<VisualizerThread> getThreadsForProcess(int processId) {
		List<VisualizerThread> result = null;
		for (VisualizerThread thread : m_threads) {
			if (thread.getPID() == processId) {
				if (result == null) result = new ArrayList<VisualizerThread>();
				result.add(thread);
			}
		}
		return result;
	}

	/** 
	 * Find a thread by GDB threadId.
	 * Since thread ids are unique across a GDB session,
	 * we can uniquely find a thread based on its id.
	 */
	public VisualizerThread getThread(int threadId) {
		VisualizerThread result = null;
		for (VisualizerThread thread : m_threads) {
			if (thread.getGDBTID() == threadId) {
				result = thread;
				break;
			}
		}
		return result;
	}

	/** Adds thread. */
	public VisualizerThread addThread(VisualizerThread thread) {
		m_threads.add(thread);
		return thread;
	}

	/** Removes thread. */
	public void removeThread(VisualizerThread thread) {
		m_threads.remove(thread);
	}

	/** 
	 * Removes thread by GDB threadId.
	 */
	public void removeThread(int threadId) {
		Iterator<VisualizerThread> itr = m_threads.iterator();
		while (itr.hasNext()) {
			VisualizerThread thread = itr.next();
			if (thread.getGDBTID() == threadId) {
				itr.remove();
				break;
			}
		}
	}
	
	/**
	 * Mark the specified thread as having exited.
	 */
	public void markThreadExited(int threadId) {
		if (m_keepExitedThreads) {
			VisualizerThread thread = getThread(threadId);
			thread.setState(VisualizerExecutionState.EXITED);
		} else {
			removeThread(threadId);
		}
	}
}

Back to the top