lttng: Add System Call Analysis benchmarks
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / AttributeTree.java
CommitLineData
a52fde77 1/*******************************************************************************
c44f0a0c 2 * Copyright (c) 2012, 2016 Ericsson
a52fde77
AM
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
8d1346f0 5 *
a52fde77
AM
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
8d1346f0 10 *
e13bd4cd
PT
11 * Contributors:
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
a52fde77
AM
14 *******************************************************************************/
15
e894a508 16package org.eclipse.tracecompass.internal.statesystem.core;
a52fde77 17
04927a83 18import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
c44f0a0c
PT
19import static org.eclipse.tracecompass.statesystem.core.ITmfStateSystem.INVALID_ATTRIBUTE;
20import static org.eclipse.tracecompass.statesystem.core.ITmfStateSystem.ROOT_ATTRIBUTE;
04927a83 21
085a78a8 22import java.io.BufferedInputStream;
085a78a8
AM
23import java.io.File;
24import java.io.FileInputStream;
da66cc75 25import java.io.FileOutputStream;
085a78a8 26import java.io.IOException;
da66cc75
AM
27import java.io.ObjectInputStream;
28import java.io.ObjectOutputStream;
085a78a8 29import java.io.PrintWriter;
da66cc75 30import java.nio.channels.FileChannel;
a52fde77 31import java.util.ArrayList;
a52fde77
AM
32import java.util.List;
33
50a47aa6 34import org.eclipse.jdt.annotation.NonNull;
c44f0a0c 35import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
e894a508 36import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
6d08acca 37
a52fde77
AM
38/**
39 * The Attribute Tree is the /proc-like filesystem used to organize attributes.
40 * Each node of this tree is both like a file and a directory in the
41 * "file system".
8d1346f0 42 *
a52fde77 43 * @author alexmont
8d1346f0 44 *
a52fde77 45 */
cb42195c 46public final class AttributeTree {
a52fde77
AM
47
48 /* "Magic number" for attribute tree files or file sections */
cb42195c 49 private static final int ATTRIB_TREE_MAGIC_NUMBER = 0x06EC3671;
a52fde77
AM
50
51 private final StateSystem ss;
52 private final List<Attribute> attributeList;
53 private final Attribute attributeTreeRoot;
54
55 /**
56 * Standard constructor, create a new empty Attribute Tree
8d1346f0 57 *
a52fde77
AM
58 * @param ss
59 * The StateSystem to which this AT is attached
60 */
a6917276 61 public AttributeTree(StateSystem ss) {
a52fde77 62 this.ss = ss;
a69a9003 63 this.attributeList = new ArrayList<>();
c44f0a0c 64 this.attributeTreeRoot = new Attribute(null, "root", ROOT_ATTRIBUTE); //$NON-NLS-1$
a52fde77
AM
65 }
66
67 /**
6f04e06c
EB
68 * "Existing file" constructor. Builds an attribute tree from a
69 * "mapping file" or mapping section previously saved somewhere.
8d1346f0 70 *
a52fde77
AM
71 * @param ss
72 * StateSystem to which this AT is attached
73 * @param fis
74 * File stream where to read the AT information. Make sure it's
6f04e06c 75 * sought at the right place!
a52fde77 76 * @throws IOException
a6917276 77 * If there is a problem reading from the file stream
a52fde77 78 */
a6917276 79 public AttributeTree(StateSystem ss, FileInputStream fis) throws IOException {
a52fde77 80 this(ss);
da66cc75 81 ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
a52fde77
AM
82
83 /* Read the header of the Attribute Tree file (or file section) */
da66cc75 84 int res = ois.readInt(); /* Magic number */
a52fde77 85 if (res != ATTRIB_TREE_MAGIC_NUMBER) {
da66cc75 86 throw new IOException("The attribute tree file section is either invalid or corrupted."); //$NON-NLS-1$
a52fde77
AM
87 }
88
a52fde77 89
da66cc75
AM
90 ArrayList<String[]> attribList;
91 try {
92 @SuppressWarnings("unchecked")
93 ArrayList<String[]> list = (ArrayList<String[]>) ois.readObject();
94 attribList = list;
95 } catch (ClassNotFoundException e) {
96 throw new IOException("Unrecognizable attribute list"); //$NON-NLS-1$
a52fde77
AM
97 }
98
99 /*
100 * Now we have 'list', the ArrayList of String arrays representing all
101 * the attributes. Simply create attributes the normal way from them.
102 */
da66cc75 103 for (String[] attrib : attribList) {
c44f0a0c 104 this.getQuarkAndAdd(ROOT_ATTRIBUTE, attrib);
a52fde77
AM
105 }
106 }
107
108 /**
a6917276 109 * Tell the Attribute Tree to write itself somewhere in a file.
8d1346f0 110 *
a6917276
AM
111 * @param file
112 * The file to write to
113 * @param pos
114 * The position (in bytes) in the file where to write
a52fde77 115 */
a69a9003 116 public synchronized void writeSelf(File file, long pos) {
da66cc75
AM
117 try (FileOutputStream fos = new FileOutputStream(file, true);
118 FileChannel fc = fos.getChannel();) {
119 fc.position(pos);
120 try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
121
122 /* Write the almost-magic number */
123 oos.writeInt(ATTRIB_TREE_MAGIC_NUMBER);
124
125 /* Compute the serialized list of attributes and write it */
126 List<String[]> list = new ArrayList<>(attributeList.size());
127 for (Attribute entry : this.attributeList) {
128 list.add(entry.getFullAttribute());
a52fde77 129 }
da66cc75 130 oos.writeObject(list);
a52fde77 131 }
a52fde77
AM
132 } catch (IOException e) {
133 e.printStackTrace();
134 }
da66cc75 135
a52fde77
AM
136 }
137
138 /**
139 * Return the number of attributes this system as seen so far. Note that
140 * this also equals the integer value (quark) the next added attribute will
141 * have.
8d1346f0 142 *
a6917276 143 * @return The current number of attributes in the tree
a52fde77 144 */
a69a9003 145 public synchronized int getNbAttributes() {
a52fde77
AM
146 return attributeList.size();
147 }
148
149 /**
a6917276 150 * Get the quark for a given attribute path. No new attribute will be
c44f0a0c
PT
151 * created : if the specified path does not exist, return
152 * {@link ITmfStateSystem#INVALID_ATTRIBUTE}.
8d1346f0 153 *
a6917276
AM
154 * @param startingNodeQuark
155 * The quark of the attribute from which relative queries will
c44f0a0c
PT
156 * start. Use {@link ITmfStateSystem#ROOT_ATTRIBUTE} to start at
157 * the root node.
a6917276
AM
158 * @param subPath
159 * The path to the attribute, relative to the starting node.
c44f0a0c
PT
160 * @return The quark of the specified attribute, or
161 * {@link ITmfStateSystem#INVALID_ATTRIBUTE} if that attribute does
162 * not exist.
a52fde77 163 */
c44f0a0c
PT
164 public synchronized int getQuarkDontAdd(int startingNodeQuark, String... subPath) {
165 assert (startingNodeQuark >= ROOT_ATTRIBUTE);
a52fde77
AM
166
167 Attribute prevNode;
168
6abc2d88
AM
169 /* If subPath is empty, simply return the starting quark */
170 if (subPath == null || subPath.length == 0) {
171 return startingNodeQuark;
172 }
173
a52fde77 174 /* Get the "starting node" */
c44f0a0c 175 if (startingNodeQuark == ROOT_ATTRIBUTE) {
a52fde77
AM
176 prevNode = attributeTreeRoot;
177 } else {
178 prevNode = attributeList.get(startingNodeQuark);
179 }
180
c44f0a0c 181 return prevNode.getSubAttributeQuark(subPath);
a52fde77
AM
182 }
183
a6917276
AM
184 /**
185 * Get the quark of a given attribute path. If that specified path does not
186 * exist, it will be created (and the quark that was just created will be
187 * returned).
188 *
189 * @param startingNodeQuark
190 * The quark of the attribute from which relative queries will
c44f0a0c
PT
191 * start. Use {@link ITmfStateSystem#ROOT_ATTRIBUTE} to start at
192 * the root node.
a6917276
AM
193 * @param subPath
194 * The path to the attribute, relative to the starting node.
195 * @return The quark of the attribute represented by the path
196 */
197 public synchronized int getQuarkAndAdd(int startingNodeQuark, String... subPath) {
198 // FIXME synchronized here is probably quite costly... maybe only locking
199 // the "for" would be enough?
a52fde77 200 assert (subPath != null && subPath.length > 0);
c44f0a0c 201 assert (startingNodeQuark >= ROOT_ATTRIBUTE);
a52fde77
AM
202
203 Attribute nextNode = null;
204 Attribute prevNode;
205
206 /* Get the "starting node" */
c44f0a0c 207 if (startingNodeQuark == ROOT_ATTRIBUTE) {
a52fde77
AM
208 prevNode = attributeTreeRoot;
209 } else {
210 prevNode = attributeList.get(startingNodeQuark);
211 }
212
213 int knownQuark = prevNode.getSubAttributeQuark(subPath);
c44f0a0c 214 if (knownQuark == INVALID_ATTRIBUTE) {
a52fde77
AM
215 /*
216 * The attribute was not in the table previously, and we want to add
217 * it
218 */
219 for (String curDirectory : subPath) {
220 nextNode = prevNode.getSubAttributeNode(curDirectory);
221 if (nextNode == null) {
222 /* This is where we need to start adding */
04927a83 223 nextNode = new Attribute(prevNode, checkNotNull(curDirectory), attributeList.size());
a52fde77
AM
224 prevNode.addSubAttribute(nextNode);
225 attributeList.add(nextNode);
8d1346f0 226 ss.addEmptyAttribute();
a52fde77
AM
227 }
228 prevNode = nextNode;
229 }
085a78a8 230 return attributeList.size() - 1;
a52fde77
AM
231 }
232 /*
233 * The attribute was already existing, return the quark of that
234 * attribute
235 */
236 return knownQuark;
237 }
238
0a9de3d2
AM
239 /**
240 * Returns the sub-attributes of the quark passed in parameter
8d1346f0 241 *
0a9de3d2 242 * @param attributeQuark
a6917276 243 * The quark of the attribute to print the sub-attributes of.
c66426fd 244 * @param recursive
a6917276
AM
245 * Should the query be recursive or not? If false, only children
246 * one level deep will be returned. If true, all descendants will
247 * be returned (depth-first search)
248 * @return The list of quarks representing the children attributes
0a9de3d2 249 * @throws AttributeNotFoundException
c44f0a0c 250 * If 'attributeQuark' is invalid, or if there is no attribute
a6917276 251 * associated to it.
0a9de3d2 252 */
df2597e0 253 public synchronized @NonNull List<@NonNull Integer> getSubAttributes(int attributeQuark, boolean recursive)
0a9de3d2 254 throws AttributeNotFoundException {
df2597e0 255 List<@NonNull Integer> listOfChildren = new ArrayList<>();
0a9de3d2 256 Attribute startingAttribute;
c66426fd 257
0a9de3d2 258 /* Check if the quark is valid */
c44f0a0c 259 if (attributeQuark < ROOT_ATTRIBUTE || attributeQuark >= attributeList.size()) {
e13bd4cd 260 throw new AttributeNotFoundException(ss.getSSID() + " Quark:" + attributeQuark); //$NON-NLS-1$
0a9de3d2 261 }
c66426fd 262
0a9de3d2 263 /* Set up the node from which we'll start the search */
c44f0a0c 264 if (attributeQuark == ROOT_ATTRIBUTE) {
0a9de3d2
AM
265 startingAttribute = attributeTreeRoot;
266 } else {
267 startingAttribute = attributeList.get(attributeQuark);
268 }
c66426fd 269
0a9de3d2 270 /* Iterate through the sub-attributes and add them to the list */
c66426fd 271 addSubAttributes(listOfChildren, startingAttribute, recursive);
0a9de3d2 272
a52fde77
AM
273 return listOfChildren;
274 }
275
0fdd2c45
FG
276 /**
277 * Returns the parent quark of the attribute. The root attribute has no
c44f0a0c 278 * parent and will return {@link ITmfStateSystem#ROOT_ATTRIBUTE}.
0fdd2c45
FG
279 *
280 * @param quark
281 * The quark of the attribute
c44f0a0c
PT
282 * @return Quark of the parent attribute or
283 * {@link ITmfStateSystem#ROOT_ATTRIBUTE} for the root attribute
0fdd2c45 284 */
a69a9003 285 public synchronized int getParentAttributeQuark(int quark) {
c44f0a0c 286 if (quark == ROOT_ATTRIBUTE) {
0fdd2c45
FG
287 return quark;
288 }
289 return attributeList.get(quark).getParentAttributeQuark();
290 }
291
c66426fd
AM
292 private void addSubAttributes(List<Integer> list, Attribute curAttribute,
293 boolean recursive) {
cb42195c 294 for (Attribute childNode : curAttribute.getSubAttributes()) {
c66426fd
AM
295 list.add(childNode.getQuark());
296 if (recursive) {
297 addSubAttributes(list, childNode, true);
298 }
299 }
300 }
301
a6917276
AM
302 /**
303 * Get then base name of an attribute specified by a quark.
304 *
305 * @param quark
306 * The quark of the attribute
307 * @return The (base) name of the attribute
308 */
a69a9003 309 public synchronized @NonNull String getAttributeName(int quark) {
50678114
AM
310 return attributeList.get(quark).getName();
311 }
312
a6917276
AM
313 /**
314 * Get the full path name of an attribute specified by a quark.
315 *
316 * @param quark
317 * The quark of the attribute
318 * @return The full path name of the attribute
319 */
a69a9003 320 public synchronized @NonNull String getFullAttributeName(int quark) {
a52fde77
AM
321 return attributeList.get(quark).getFullAttributeName();
322 }
323
34638411
AM
324 /**
325 * Get the full path name (as an array of path elements) of an attribute
326 * specified by a quark.
327 *
328 * @param quark
329 * The quark of the attribute
330 * @return The path elements of the full path
331 */
4c4e2816 332 public synchronized String @NonNull [] getFullAttributePathArray(int quark) {
34638411
AM
333 return attributeList.get(quark).getFullAttribute();
334 }
335
a6917276
AM
336 /**
337 * Debug-print all the attributes in the tree.
338 *
339 * @param writer
340 * The writer where to print the output
341 */
a69a9003 342 public synchronized void debugPrint(PrintWriter writer) {
a52fde77
AM
343 attributeTreeRoot.debugPrint(writer);
344 }
345
a6917276 346}
This page took 0.094956 seconds and 5 git commands to generate.