~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-13 21:43:22 UTC
  • mfrom: (1.2.1) (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130513214322-6frgd9du1n0w2uo7
Tags: 1.2.1-1
* Team upload.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
package org.eclipse.linuxtools.internal.tmf.core.statesystem;
14
14
 
 
15
import java.io.File;
 
16
import java.io.IOException;
15
17
import java.io.PrintWriter;
 
18
import java.util.ArrayList;
16
19
import java.util.LinkedList;
17
20
import java.util.List;
18
21
 
 
22
import org.eclipse.core.runtime.IProgressMonitor;
 
23
import org.eclipse.core.runtime.NullProgressMonitor;
 
24
import org.eclipse.linuxtools.internal.tmf.core.Tracer;
19
25
import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
20
26
import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
21
27
import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
 
28
import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
 
29
import org.eclipse.linuxtools.tmf.core.interval.TmfStateInterval;
 
30
import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemBuilder;
 
31
import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier2;
22
32
import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
23
33
import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
24
34
 
25
35
/**
26
 
 * This is the base class for the StateHistorySystem. It contains all the
27
 
 * current-state-updating methods.
28
 
 *
29
 
 * It's not abstract, as it can be used by itself: in this case, no History tree
30
 
 * will be built underneath (no information will be saved to disk) and it will
31
 
 * only be able to respond to queries to the current, latest time.
32
 
 *
33
 
 * (See IStateSystemQuerier and IStateSystemBuilder for the Javadoc.)
 
36
 * This is the core class of the Generic State System. It contains all the
 
37
 * methods to build and query a state history. It's exposed externally through
 
38
 * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the
 
39
 * user needs read-only access or read-write access.
 
40
 *
 
41
 * When building, DON'T FORGET to call .closeHistory() when you are done
 
42
 * inserting intervals, or the storage backend will have no way of knowing it
 
43
 * can close and write itself to disk, and its thread will keep running.
34
44
 *
35
45
 * @author alexmont
36
46
 *
37
47
 */
38
 
@SuppressWarnings("javadoc") // the javadoc is in the IStateSystem* interfaces
39
 
public class StateSystem {
 
48
public class StateSystem implements IStateSystemBuilder, IStateSystemQuerier2{
40
49
 
41
50
    /* References to the inner structures */
42
 
    protected AttributeTree attributeTree;
43
 
    protected TransientState transState;
44
 
 
45
 
    /**
46
 
     * Constructor. No configuration needed!
47
 
     */
48
 
    public StateSystem() {
49
 
        attributeTree = new AttributeTree(this);
50
 
 
51
 
        /* This will tell the builder to discard the intervals */
52
 
        transState = new TransientState(null);
53
 
    }
54
 
 
 
51
    private final AttributeTree attributeTree;
 
52
    private final TransientState transState;
 
53
    private final IStateHistoryBackend backend;
 
54
 
 
55
    /**
 
56
     * General constructor
 
57
     *
 
58
     * @param backend
 
59
     *            The "state history storage" backend to use.
 
60
     * @param newFile
 
61
     *            Put true if this is a new history started from scratch. It is
 
62
     *            used to tell the state system where to get its attribute tree.
 
63
     * @throws IOException
 
64
     *             If there was a problem creating the new history file
 
65
     */
 
66
    public StateSystem(IStateHistoryBackend backend, boolean newFile)
 
67
            throws IOException {
 
68
        this.backend = backend;
 
69
        this.transState = new TransientState(backend);
 
70
 
 
71
        if (newFile) {
 
72
            attributeTree = new AttributeTree(this);
 
73
        } else {
 
74
            /* We're opening an existing file */
 
75
            this.attributeTree = new AttributeTree(this, backend.supplyAttributeTreeReader());
 
76
            transState.setInactive();
 
77
        }
 
78
    }
 
79
 
 
80
    //--------------------------------------------------------------------------
 
81
    //        General methods related to the attribute tree
 
82
    //--------------------------------------------------------------------------
 
83
 
 
84
    /**
 
85
     * Method used by the attribute tree when creating new attributes, to keep
 
86
     * the attribute count in the transient state in sync.
 
87
     */
 
88
    void addEmptyAttribute() {
 
89
        transState.addEmptyEntry();
 
90
    }
 
91
 
 
92
    @Override
55
93
    public int getNbAttributes() {
56
94
        return attributeTree.getNbAttributes();
57
95
    }
58
96
 
59
 
    /**
60
 
     * @name Quark-retrieving methods
61
 
     */
62
 
 
 
97
    @Override
 
98
    public String getAttributeName(int attributeQuark) {
 
99
        return attributeTree.getAttributeName(attributeQuark);
 
100
    }
 
101
 
 
102
    @Override
 
103
    public String getFullAttributePath(int attributeQuark) {
 
104
        return attributeTree.getFullAttributeName(attributeQuark);
 
105
    }
 
106
 
 
107
    //--------------------------------------------------------------------------
 
108
    //        Methods related to the storage backend
 
109
    //--------------------------------------------------------------------------
 
110
 
 
111
    @Override
 
112
    public long getStartTime() {
 
113
        return backend.getStartTime();
 
114
    }
 
115
 
 
116
    @Override
 
117
    public long getCurrentEndTime() {
 
118
        return backend.getEndTime();
 
119
    }
 
120
 
 
121
    @Override
 
122
    public void closeHistory(long endTime) throws TimeRangeException {
 
123
        File attributeTreeFile;
 
124
        long attributeTreeFilePos;
 
125
        long realEndTime = endTime;
 
126
 
 
127
        if (realEndTime < backend.getEndTime()) {
 
128
            /*
 
129
             * This can happen (empty nodes pushing the border further, etc.)
 
130
             * but shouldn't be too big of a deal.
 
131
             */
 
132
            realEndTime = backend.getEndTime();
 
133
        }
 
134
        transState.closeTransientState(realEndTime);
 
135
        backend.finishedBuilding(realEndTime);
 
136
 
 
137
        attributeTreeFile = backend.supplyAttributeTreeWriterFile();
 
138
        attributeTreeFilePos = backend.supplyAttributeTreeWriterFilePosition();
 
139
        if (attributeTreeFile != null) {
 
140
            /*
 
141
             * If null was returned, we simply won't save the attribute tree,
 
142
             * too bad!
 
143
             */
 
144
            attributeTree.writeSelf(attributeTreeFile, attributeTreeFilePos);
 
145
        }
 
146
    }
 
147
 
 
148
    //--------------------------------------------------------------------------
 
149
    //        Quark-retrieving methods
 
150
    //--------------------------------------------------------------------------
 
151
 
 
152
    @Override
63
153
    public int getQuarkAbsolute(String... attribute)
64
154
            throws AttributeNotFoundException {
65
155
        return attributeTree.getQuarkDontAdd(-1, attribute);
66
156
    }
67
157
 
 
158
    @Override
68
159
    public int getQuarkAbsoluteAndAdd(String... attribute) {
69
160
        return attributeTree.getQuarkAndAdd(-1, attribute);
70
161
    }
71
162
 
 
163
    @Override
72
164
    public int getQuarkRelative(int startingNodeQuark, String... subPath)
73
165
            throws AttributeNotFoundException {
74
166
        return attributeTree.getQuarkDontAdd(startingNodeQuark, subPath);
75
167
    }
76
168
 
 
169
    @Override
77
170
    public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) {
78
171
        return attributeTree.getQuarkAndAdd(startingNodeQuark, subPath);
79
172
    }
80
173
 
 
174
    @Override
81
175
    public List<Integer> getSubAttributes(int quark, boolean recursive)
82
176
            throws AttributeNotFoundException {
83
177
        return attributeTree.getSubAttributes(quark, recursive);
84
178
    }
85
179
 
 
180
    @Override
86
181
    public List<Integer> getQuarks(String... pattern) {
87
182
        List<Integer> quarks = new LinkedList<Integer>();
88
183
        List<String> prefix = new LinkedList<String>();
167
262
        return quarks;
168
263
    }
169
264
 
170
 
    /**
171
 
     * @name External methods related to insertions in the history -
172
 
     */
 
265
    //--------------------------------------------------------------------------
 
266
    //        Methods related to insertions in the history
 
267
    //--------------------------------------------------------------------------
173
268
 
 
269
    @Override
174
270
    public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark)
175
271
            throws TimeRangeException, AttributeNotFoundException,
176
272
            StateValueTypeException {
177
273
        transState.processStateChange(t, value, attributeQuark);
178
274
    }
179
275
 
 
276
    @Override
180
277
    public void incrementAttribute(long t, int attributeQuark)
181
278
            throws StateValueTypeException, TimeRangeException,
182
279
            AttributeNotFoundException {
183
280
        int prevValue = queryOngoingState(attributeQuark).unboxInt();
184
 
        /* prevValue should be == 0 if the attribute wasn't existing before */
 
281
        if (prevValue == -1) {
 
282
            /* if the attribute was previously null, start counting at 0 */
 
283
            prevValue = 0;
 
284
        }
185
285
        modifyAttribute(t, TmfStateValue.newValueInt(prevValue + 1),
186
286
                attributeQuark);
187
287
    }
188
288
 
 
289
    @Override
189
290
    public void pushAttribute(long t, ITmfStateValue value, int attributeQuark)
190
291
            throws TimeRangeException, AttributeNotFoundException,
191
292
            StateValueTypeException {
224
325
        modifyAttribute(t, value, subAttributeQuark);
225
326
    }
226
327
 
 
328
    @Override
227
329
    public void popAttribute(long t, int attributeQuark)
228
330
            throws AttributeNotFoundException, TimeRangeException,
229
331
            StateValueTypeException {
272
374
        removeAttribute(t, subAttributeQuark);
273
375
    }
274
376
 
 
377
    @Override
275
378
    public void removeAttribute(long t, int attributeQuark)
276
379
            throws TimeRangeException, AttributeNotFoundException {
277
380
        assert (attributeQuark >= 0);
299
402
        }
300
403
    }
301
404
 
302
 
    /**
303
 
     * @name "Current" query/update methods -
304
 
     */
 
405
    //--------------------------------------------------------------------------
 
406
    //        "Current" query/update methods
 
407
    //--------------------------------------------------------------------------
305
408
 
 
409
    @Override
306
410
    public ITmfStateValue queryOngoingState(int attributeQuark)
307
411
            throws AttributeNotFoundException {
308
412
        return transState.getOngoingStateValue(attributeQuark);
309
413
    }
310
414
 
 
415
    @Override
311
416
    public void updateOngoingState(ITmfStateValue newValue, int attributeQuark)
312
417
            throws AttributeNotFoundException {
313
418
        transState.changeOngoingStateValue(attributeQuark, newValue);
314
419
    }
315
420
 
316
 
    public String getAttributeName(int attributeQuark) {
317
 
        return attributeTree.getAttributeName(attributeQuark);
318
 
    }
319
 
 
320
 
    public String getFullAttributePath(int attributeQuark) {
321
 
        return attributeTree.getFullAttributeName(attributeQuark);
 
421
 
 
422
 
 
423
    //--------------------------------------------------------------------------
 
424
    //        Regular query methods (sent to the back-end)
 
425
    //--------------------------------------------------------------------------
 
426
 
 
427
    @Override
 
428
    public synchronized List<ITmfStateInterval> queryFullState(long t)
 
429
            throws TimeRangeException {
 
430
        List<ITmfStateInterval> stateInfo = new ArrayList<ITmfStateInterval>(
 
431
                attributeTree.getNbAttributes());
 
432
 
 
433
        /* Bring the size of the array to the current number of attributes */
 
434
        for (int i = 0; i < attributeTree.getNbAttributes(); i++) {
 
435
            stateInfo.add(null);
 
436
        }
 
437
 
 
438
        /* Query the storage backend */
 
439
        backend.doQuery(stateInfo, t);
 
440
 
 
441
        /*
 
442
         * If we are currently building the history, also query the "ongoing"
 
443
         * states for stuff that might not yet be written to the history.
 
444
         */
 
445
        if (transState.isActive()) {
 
446
            transState.doQuery(stateInfo, t);
 
447
        }
 
448
 
 
449
        /*
 
450
         * We should have previously inserted an interval for every attribute.
 
451
         * If we do happen do see a 'null' object here, just replace it with a a
 
452
         * dummy internal with a null value, to avoid NPE's further up.
 
453
         */
 
454
        for (int i = 0; i < stateInfo.size(); i++) {
 
455
            if (stateInfo.get(i) == null) {
 
456
                //logMissingInterval(i, t);
 
457
                stateInfo.set(i, new TmfStateInterval(t, t, i, TmfStateValue.nullValue()));
 
458
            }
 
459
        }
 
460
        return stateInfo;
 
461
    }
 
462
 
 
463
    @Override
 
464
    public ITmfStateInterval querySingleState(long t, int attributeQuark)
 
465
            throws AttributeNotFoundException, TimeRangeException {
 
466
        ITmfStateInterval ret;
 
467
 
 
468
        if (transState.hasInfoAboutStateOf(t, attributeQuark)) {
 
469
            ret = transState.getOngoingInterval(attributeQuark);
 
470
        } else {
 
471
            ret = backend.doSingularQuery(t, attributeQuark);
 
472
        }
 
473
 
 
474
        /*
 
475
         * Return a fake interval if we could not find anything in the history.
 
476
         * We do NOT want to return 'null' here.
 
477
         */
 
478
        if (ret == null) {
 
479
            //logMissingInterval(attributeQuark, t);
 
480
            return new TmfStateInterval(t, this.getCurrentEndTime(),
 
481
                    attributeQuark, TmfStateValue.nullValue());
 
482
        }
 
483
        return ret;
 
484
    }
 
485
 
 
486
    @Override
 
487
    public List<ITmfStateInterval> queryHistoryRange(int attributeQuark,
 
488
            long t1, long t2) throws TimeRangeException,
 
489
            AttributeNotFoundException {
 
490
        List<ITmfStateInterval> intervals;
 
491
        ITmfStateInterval currentInterval;
 
492
        long ts, tEnd;
 
493
 
 
494
        /* Make sure the time range makes sense */
 
495
        if (t2 <= t1) {
 
496
            throw new TimeRangeException();
 
497
        }
 
498
 
 
499
        /* Set the actual, valid end time of the range query */
 
500
        if (t2 > this.getCurrentEndTime()) {
 
501
            tEnd = this.getCurrentEndTime();
 
502
        } else {
 
503
            tEnd = t2;
 
504
        }
 
505
 
 
506
        /* Get the initial state at time T1 */
 
507
        intervals = new ArrayList<ITmfStateInterval>();
 
508
        currentInterval = querySingleState(t1, attributeQuark);
 
509
        intervals.add(currentInterval);
 
510
 
 
511
        /* Get the following state changes */
 
512
        ts = currentInterval.getEndTime();
 
513
        while (ts != -1 && ts < tEnd) {
 
514
            ts++; /* To "jump over" to the next state in the history */
 
515
            currentInterval = querySingleState(ts, attributeQuark);
 
516
            intervals.add(currentInterval);
 
517
            ts = currentInterval.getEndTime();
 
518
        }
 
519
        return intervals;
 
520
    }
 
521
 
 
522
    @Override
 
523
    public List<ITmfStateInterval> queryHistoryRange(int attributeQuark,
 
524
            long t1, long t2, long resolution) throws TimeRangeException,
 
525
            AttributeNotFoundException {
 
526
        return queryHistoryRange(attributeQuark, t1, t2, resolution, new NullProgressMonitor());
 
527
    }
 
528
 
 
529
    @Override
 
530
    public List<ITmfStateInterval> queryHistoryRange(int attributeQuark,
 
531
            long t1, long t2, long resolution, IProgressMonitor monitor) throws TimeRangeException,
 
532
            AttributeNotFoundException {
 
533
        List<ITmfStateInterval> intervals;
 
534
        ITmfStateInterval currentInterval;
 
535
        long ts, tEnd;
 
536
 
 
537
        /* Make sure the time range makes sense */
 
538
        if (t2 < t1 || resolution <= 0) {
 
539
            throw new TimeRangeException();
 
540
        }
 
541
 
 
542
        /* Set the actual, valid end time of the range query */
 
543
        if (t2 > this.getCurrentEndTime()) {
 
544
            tEnd = this.getCurrentEndTime();
 
545
        } else {
 
546
            tEnd = t2;
 
547
        }
 
548
 
 
549
        /* Get the initial state at time T1 */
 
550
        intervals = new ArrayList<ITmfStateInterval>();
 
551
        currentInterval = querySingleState(t1, attributeQuark);
 
552
        intervals.add(currentInterval);
 
553
 
 
554
        /*
 
555
         * Iterate over the "resolution points". We skip unneeded queries in the
 
556
         * case the current interval is longer than the resolution.
 
557
         */
 
558
        for (ts = t1; (currentInterval.getEndTime() != -1) && (ts < tEnd);
 
559
                ts += resolution) {
 
560
            if (monitor.isCanceled()) {
 
561
                return intervals;
 
562
            }
 
563
            if (ts <= currentInterval.getEndTime()) {
 
564
                continue;
 
565
            }
 
566
            currentInterval = querySingleState(ts, attributeQuark);
 
567
            intervals.add(currentInterval);
 
568
        }
 
569
 
 
570
        /* Add the interval at t2, if it wasn't included already. */
 
571
        if (currentInterval.getEndTime() < tEnd) {
 
572
            currentInterval = querySingleState(tEnd, attributeQuark);
 
573
            intervals.add(currentInterval);
 
574
        }
 
575
        return intervals;
 
576
    }
 
577
 
 
578
    //--------------------------------------------------------------------------
 
579
    //        Debug methods
 
580
    //--------------------------------------------------------------------------
 
581
 
 
582
    static void logMissingInterval(int attribute, long timestamp) {
 
583
        Tracer.traceInfo("No data found in history for attribute " + //$NON-NLS-1$
 
584
                attribute + " at time " + timestamp + //$NON-NLS-1$
 
585
                ", returning dummy interval"); //$NON-NLS-1$
322
586
    }
323
587
 
324
588
    /**
330
594
    public void debugPrint(PrintWriter writer) {
331
595
        attributeTree.debugPrint(writer);
332
596
        transState.debugPrint(writer);
 
597
        backend.debugPrint(writer);
333
598
    }
334
599
 
335
 
}
 
 
b'\\ No newline at end of file'
 
600
}