1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef MAIN_MODEL_LOADER_H
6#define MAIN_MODEL_LOADER_H
7
8
9#include <util/DoublyLinkedList.h>
10
11#include "AbstractModelLoader.h"
12#include "Model.h"
13
14
15class BDataIO;
16class BDebugEventInputStream;
17class DataSource;
18struct system_profiler_thread_added;
19
20
21class ModelLoader : public AbstractModelLoader {
22public:
23								ModelLoader(DataSource* dataSource,
24									const BMessenger& target,
25									void* targetCookie);
26
27protected:
28								~ModelLoader();
29
30public:
31			Model*				DetachModel();
32
33protected:
34	virtual	status_t			PrepareForLoading();
35	virtual	status_t			Load();
36	virtual	void				FinishLoading(bool success);
37
38private:
39			// shorthands for the longish structure names
40			typedef system_profiler_thread_enqueued_in_run_queue
41				thread_enqueued_in_run_queue;
42			typedef system_profiler_thread_removed_from_run_queue
43				thread_removed_from_run_queue;
44			typedef system_profiler_io_request_scheduled io_request_scheduled;
45			typedef system_profiler_io_request_finished io_request_finished;
46			typedef system_profiler_io_operation_started io_operation_started;
47			typedef system_profiler_io_operation_finished io_operation_finished;
48
49			struct CPUInfo;
50			struct IOOperation;
51			struct IORequest;
52			struct IORequestHashDefinition;
53			struct ExtendedThreadSchedulingState;
54			struct ExtendedSchedulingState;
55
56			typedef DoublyLinkedList<ModelLoader::IOOperation> IOOperationList;
57			typedef DoublyLinkedList<ModelLoader::IORequest> IORequestList;
58			typedef BOpenHashTable<IORequestHashDefinition> IORequestTable;
59
60private:
61			status_t			_Load();
62			status_t			_ReadDebugEvents(void** _eventData,
63									size_t* _size);
64			status_t			_CreateDebugEventArray(void* eventData,
65									size_t eventDataSize,
66									system_profiler_event_header**& _events,
67									size_t& _eventCount);
68			status_t			_ProcessEvent(uint32 event, uint32 cpu,
69									const void* buffer, size_t size);
70			bool				_SetThreadEvents();
71			bool				_SetThreadIORequests();
72			void				_SetThreadIORequests(Model::Thread* thread,
73									Model::IORequest** requests,
74									size_t requestCount);
75
76	inline	void				_UpdateLastEventTime(nanotime_t time);
77
78			void				_HandleTeamAdded(
79									system_profiler_team_added* event);
80			void				_HandleTeamRemoved(
81									system_profiler_team_removed* event);
82			void				_HandleTeamExec(
83									system_profiler_team_exec* event);
84			void				_HandleThreadAdded(
85									system_profiler_thread_added* event);
86			void				_HandleThreadRemoved(
87									system_profiler_thread_removed* event);
88			void				_HandleThreadScheduled(uint32 cpu,
89									system_profiler_thread_scheduled* event);
90			void				_HandleThreadEnqueuedInRunQueue(
91									thread_enqueued_in_run_queue* event);
92			void				_HandleThreadRemovedFromRunQueue(uint32 cpu,
93									thread_removed_from_run_queue* event);
94			void				_HandleWaitObjectInfo(
95									system_profiler_wait_object_info* event);
96			void				_HandleIOSchedulerAdded(
97									system_profiler_io_scheduler_added* event);
98			void				_HandleIORequestScheduled(
99									io_request_scheduled* event);
100			void				_HandleIORequestFinished(
101									io_request_finished* event);
102			void				_HandleIOOperationStarted(
103									io_operation_started* event);
104			void				_HandleIOOperationFinished(
105									io_operation_finished* event);
106
107			ExtendedThreadSchedulingState* _AddThread(
108									system_profiler_thread_added* event);
109			ExtendedThreadSchedulingState* _AddUnknownThread(
110									thread_id threadID);
111			Model::Team*		_AddUnknownTeam();
112
113			void				_AddThreadWaitObject(
114									ExtendedThreadSchedulingState* thread,
115									uint32 type, addr_t object);
116
117			void				_AddIdleTime(uint32 cpu, nanotime_t time);
118
119private:
120			Model*				fModel;
121			DataSource*			fDataSource;
122			CPUInfo*			fCPUInfos;
123			nanotime_t			fBaseTime;
124			ExtendedSchedulingState* fState;
125			IORequestTable*		fIORequests;
126			uint32				fMaxCPUIndex;
127};
128
129
130#endif	// MAIN_MODEL_LOADER_H
131