blob: e370127ec8b19a99a7d9b3122f4e33951c672232 [file] [log] [blame]
Hector Dearman916c7612018-07-09 15:42:22 +01001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020015import {assertExists} from '../base/logging';
Hector Dearmana16f8a92018-07-18 17:29:09 +010016import {Action} from '../common/actions';
Primiano Tuccie36ca632018-08-21 14:32:23 +020017import {createEmptyState, State} from '../common/state';
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020018
Deepanjan Roy9d95a252018-08-09 10:10:19 -040019import {FrontendLocalState} from './frontend_local_state';
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020020import {RafScheduler} from './raf_scheduler';
Hector Dearman306a5392018-07-26 15:29:42 +010021
Hector Dearmana16f8a92018-07-18 17:29:09 +010022type Dispatch = (action: Action) => void;
Deepanjan Roy56fa86b2018-08-02 10:25:56 -040023type TrackDataStore = Map<string, {}>;
Primiano Tucci8afc06d2018-08-06 19:11:42 +010024type QueryResultsStore = Map<string, {}>;
Hector Dearmana16f8a92018-07-18 17:29:09 +010025
Primiano Tuccie36ca632018-08-21 14:32:23 +020026export interface QuantizedLoad {
27 startSec: number;
28 endSec: number;
29 load: number;
30}
31type OverviewStore = Map<string, QuantizedLoad[]>;
32
33export interface ThreadDesc {
34 utid: number;
35 tid: number;
36 threadName: string;
37 pid: number;
38 procName: string;
39}
40type ThreadMap = Map<number, ThreadDesc>;
41
Hector Dearman916c7612018-07-09 15:42:22 +010042/**
Hector Dearmana16f8a92018-07-18 17:29:09 +010043 * Global accessors for state/dispatch in the frontend.
Hector Dearman916c7612018-07-09 15:42:22 +010044 */
Hector Dearmana16f8a92018-07-18 17:29:09 +010045class Globals {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020046 private _dispatch?: Dispatch = undefined;
47 private _state?: State = undefined;
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020048 private _frontendLocalState?: FrontendLocalState = undefined;
49 private _rafScheduler?: RafScheduler = undefined;
Hector Dearman667982b2018-09-20 17:02:51 +010050
51 // TODO(hjd): Unify trackDataStore, queryResults, overviewStore, threads.
52 private _trackDataStore?: TrackDataStore = undefined;
53 private _queryResults?: QueryResultsStore = undefined;
Primiano Tuccie36ca632018-08-21 14:32:23 +020054 private _overviewStore?: OverviewStore = undefined;
55 private _threadMap?: ThreadMap = undefined;
Hector Dearmana38bd902018-08-02 10:38:41 +010056
Primiano Tuccie36ca632018-08-21 14:32:23 +020057 initialize(dispatch?: Dispatch) {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020058 this._dispatch = dispatch;
Primiano Tuccie36ca632018-08-21 14:32:23 +020059 this._state = createEmptyState();
Primiano Tuccie36ca632018-08-21 14:32:23 +020060 this._frontendLocalState = new FrontendLocalState();
61 this._rafScheduler = new RafScheduler();
Hector Dearman667982b2018-09-20 17:02:51 +010062
63 // TODO(hjd): Unify trackDataStore, queryResults, overviewStore, threads.
64 this._trackDataStore = new Map<string, {}>();
65 this._queryResults = new Map<string, {}>();
Primiano Tuccie36ca632018-08-21 14:32:23 +020066 this._overviewStore = new Map<string, QuantizedLoad[]>();
67 this._threadMap = new Map<number, ThreadDesc>();
Hector Dearmana16f8a92018-07-18 17:29:09 +010068 }
69
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020070 get state(): State {
71 return assertExists(this._state);
72 }
73
74 set state(state: State) {
75 this._state = assertExists(state);
Hector Dearmana16f8a92018-07-18 17:29:09 +010076 }
77
78 get dispatch(): Dispatch {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020079 return assertExists(this._dispatch);
Hector Dearmana16f8a92018-07-18 17:29:09 +010080 }
81
Hector Dearman667982b2018-09-20 17:02:51 +010082 get frontendLocalState() {
83 return assertExists(this._frontendLocalState);
84 }
85
86 get rafScheduler() {
87 return assertExists(this._rafScheduler);
88 }
89
90 // TODO(hjd): Unify trackDataStore, queryResults, overviewStore, threads.
Primiano Tuccie36ca632018-08-21 14:32:23 +020091 get overviewStore(): OverviewStore {
92 return assertExists(this._overviewStore);
93 }
94
Deepanjan Roy56fa86b2018-08-02 10:25:56 -040095 get trackDataStore(): TrackDataStore {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020096 return assertExists(this._trackDataStore);
Deepanjan Roy56fa86b2018-08-02 10:25:56 -040097 }
98
Primiano Tucci8afc06d2018-08-06 19:11:42 +010099 get queryResults(): QueryResultsStore {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +0200100 return assertExists(this._queryResults);
Primiano Tucci8afc06d2018-08-06 19:11:42 +0100101 }
102
Primiano Tuccie36ca632018-08-21 14:32:23 +0200103 get threads() {
104 return assertExists(this._threadMap);
105 }
106
Hector Dearmana16f8a92018-07-18 17:29:09 +0100107 resetForTesting() {
Primiano Tuccie36ca632018-08-21 14:32:23 +0200108 this._dispatch = undefined;
109 this._state = undefined;
Primiano Tuccie36ca632018-08-21 14:32:23 +0200110 this._frontendLocalState = undefined;
111 this._rafScheduler = undefined;
Hector Dearman667982b2018-09-20 17:02:51 +0100112
113 // TODO(hjd): Unify trackDataStore, queryResults, overviewStore, threads.
114 this._trackDataStore = undefined;
115 this._queryResults = undefined;
Primiano Tuccie36ca632018-08-21 14:32:23 +0200116 this._overviewStore = undefined;
Hector Dearman667982b2018-09-20 17:02:51 +0100117 this._threadMap = undefined;
Hector Dearmana16f8a92018-07-18 17:29:09 +0100118 }
119}
Hector Dearman510bbe02018-07-17 17:02:09 +0100120
Hector Dearman306a5392018-07-26 15:29:42 +0100121export const globals = new Globals();