]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOKernelReporters.h
xnu-6153.11.26.tar.gz
[apple/xnu.git] / iokit / IOKit / IOKernelReporters.h
1 /*
2 * Copyright (c) 2012-2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * FILE: IOReporter.h
31 * AUTH: Cyril & Soren (Core OS)
32 * DATE: 2012-2013 (Copyright Apple Inc.)
33 * DESC: IOReporting interfaces for I/O Kit drivers
34 *
35 */
36
37 #ifndef _IOKERNEL_REPORTERS_H_
38 #define _IOKERNEL_REPORTERS_H_
39
40 #include <machine/limits.h>
41
42 #include <IOKit/IOLib.h>
43 #include <IOKit/IOService.h>
44 #include <IOKit/IOLocks.h>
45 #include <IOKit/IOBufferMemoryDescriptor.h>
46
47 #include <IOKit/IOReportTypes.h>
48 #include <IOKit/IOKernelReportStructs.h>
49
50 typedef OSDictionary IOReportLegendEntry;
51
52 /*******************************
53 * TOC: this file contains
54 * 1. Introduction
55 * 2a. IOReporter class declaration (public & non-public members)
56 * 2b. static IOReporter methods unrelated to the class
57 * 3. IOReporter subclass declarations (public & non-public members)
58 * 4. IOReportLegend class declaration
59 *******************************/
60
61 /*!
62 * 1. Introduction
63 *
64 * IOReporting is a mechanism for I/O Kit drivers to gather statistics
65 * (or other information) and make it available to various "observers,"
66 * which are generally in user space. Requests for information come
67 * through two new IOService methods: ::configureReport(...) and
68 * ::updateReport(...). While not required (see IOReportTypes.h), drivers
69 * will generally use IOReporter subclass instances to track the requested
70 * information and respond to IOReporting requests. Drivers can use these
71 * classes to track information, either all the time or between "enable"
72 * and "disable" calls to IOService::configureReport().
73 *
74 * Available information is organized into "channels." A channel is
75 * uniquely identified by both driver (registry) ID and a 64-bit channel
76 * ID. One way drivers can advertise their channels is by publishing
77 * "legends" in the I/O Kit registry. In addition to collecting
78 * information and responding to queries, IOReporter objects can produce
79 * legend entries describing their channels. The IOReportLegend class
80 * helps manage legend entries from multiple reporter objects as well
81 * as with grouping channels logically for observers.
82 *
83 * An important basic constraint of the current implementation is that
84 * all channels reported by a particular reporter instance must share all
85 * traits except channel ID and name. Specifically, the channel type
86 * (including report class, categories, & size) and units. Additionally,
87 * IOHistogramReporter currently only supports one channel at a time.
88 *
89 * Currently, ::{configure/update}Report() can be called any time between
90 * when a driver calls registerService() and when free() is called on
91 * your driver. 12960947 tracks improvements / recommendations for
92 * correctly handling these calls during termination.
93 *
94 * Locking
95 * IOReporting only imposes concurrent access constraints when multiple
96 * threads are accessing the same object. Three levels of constraint apply
97 * depending on a method's purpose:
98 * 1. Allocation/Teardown - same-instance concurrency UNSAFE, MAY BLOCK
99 * 2. Configuration - same-instance concurrency SAFE, MAY BLOCK
100 * 3. Update - same-instance concurrency SAFE, WILL NOT BLOCK
101 *
102 * Configuration requires memory management which can block and must
103 * be invoked with interrupts ENABLED (for example, NOT in the interrupt
104 * context NOR with a spin lock -- like IOSimpleLock -- held).
105 *
106 * Updates can be performed with interrupts disabled, but clients should
107 * take into account that IOReporters' non-blocking currenency is achieved
108 * with IOSimpleLockLockDisable/UnlockEnableInterrupts(): that is, by
109 * disabling interrupts and taking a spin lock. While IOReporting will
110 * never hold a lock beyond a call into it, some time may be spent within
111 * the call spin-waiting for the lock. Clients holding their own
112 * spin locks should carefully consider the impact of IOReporting's
113 * (small) additional latency before calling it while holding a spin lock.
114 *
115 * The documentation for each method indicates any concurrency guarantees.
116 */
117
118
119 /*********************************/
120 /*** 2a. IOReporter Base Class ***/
121 /*********************************/
122
123 class IOReporter : public OSObject
124 {
125 OSDeclareDefaultStructors(IOReporter);
126
127 protected:
128 /*! @function IOReporter::init
129 * @abstract base init() method, called by subclass initWith() methods
130 *
131 * @param reportingService - IOService associated with all channels
132 * @param channelType - type info for all channels (element_idx = 0)
133 * @param unit - description applied for all channels
134 * @result true on success, false otherwise
135 *
136 * @discussion
137 * init() establishes the parameters of all channels for this reporter
138 * instance. Any channels added via addChannel() will be of this type
139 * and have this unit.
140 *
141 * IOReporter clients should use the static <subclass>::with() methods
142 * below to obtain fully-initialized reporter instances. ::free()
143 * expects ::init() to have completed successfully. On failure, any
144 * allocations are cleaned up.
145 *
146 * Locking: same-instance concurrency UNSAFE
147 */
148 virtual bool init(IOService *reportingService,
149 IOReportChannelType channelType,
150 IOReportUnit unit);
151
152 public:
153
154 /*! @function IOReporter::addChannel
155 * @abstract add an additional, similar channel to the reporter
156 *
157 * @param channelID - identifier for the channel to be added
158 * @param channelName - an optional human-readble name for the channel
159 * @result appropriate IOReturn code
160 *
161 * @discussion
162 * The reporter will allocate memory to track a new channel with the
163 * provided ID and name (if any). Its other traits (type, etc) will
164 * be those provided when the reporter was initialized. If no channel
165 * name is provided and the channelID consists solely of ASCII bytes,
166 * those bytes (ignoring any NUL bytes) will be used as the
167 * human-readable channel name in user space. The IOREPORT_MAKEID()
168 * macro in IOReportTypes.h can be used to create ASCII channel IDs.
169 *
170 * Locking: same-instance concurrency SAFE, MAY BLOCK
171 */
172 IOReturn addChannel(uint64_t channelID, const char *channelName = NULL);
173
174 /*! @function IOReporter::createLegend
175 * @abstract create a legend entry represending this reporter's channels
176 * @result An IOReportLegendEntry object or NULL on failure.
177 * @discussion
178 * All channels added to the reporter will be represented
179 * in the resulting legend entry.
180 *
181 * Legends must be published togethar as an array under the
182 * kIOReportLegendKey in the I/O Kit registry. The IOReportLegend
183 * class can be used to properly combine legend entries from multiple
184 * reporters as well as to put channels into groups of interest to
185 * observers. When published, individual legend entries share
186 * characteristics such as group and sub-group. Multiple IOReporter
187 * instances are required to produce independent legend entries which
188 * can then be published with different characteristics.
189 *
190 * Drivers wishing to publish legends should do so as part of their
191 * ::start() routine. As superclasses *may* have installed legend
192 * entries, any existing existing legend should be retrieved and
193 * IOReportLegend used to merge it with the new entries.
194 *
195 * Recommendations for best practices are forthcoming.
196 *
197 * Instead of calling createLegend on your reporter object and then
198 * appending it manually to IOReportLegend, one may prefer to call
199 * IOReportLegend::appendReporterLegend which creates and appends a
200 * reporter's IOReportLegendEntry in a single call.
201 *
202 * Locking: same-instance concurrency SAFE, MAY BLOCK
203 */
204 IOReportLegendEntry* createLegend(void);
205
206 /*! @function IOReporter::configureReport
207 * @abstract track IOService::configureReport(), provide sizing info
208 *
209 * @param channelList - channels to configure
210 * @param action - enable/disable/size, etc (see IOReportTypes.h)
211 * @param result - *incremented* for kIOReportGetDimensions
212 * @param destination - action-specific default destination
213 * @result appropriate IOReturn code
214 *
215 * @discussion
216 * Any time a reporting driver's ::configureReport method is invoked,
217 * this method should be invoked on each IOReporter that is being
218 * used by that driver to report channels in channelList.
219 *
220 * Any channels in channelList which are not tracked by this reporter
221 * are ignored. ::configureReport(kIOReportGetDimensions) expects
222 * the full size of all channels, including any reported by
223 * superclasses. It is valid to call this routine on multiple
224 * reporter objects in succession and they will increment 'result'
225 * to provide the correct total.
226 *
227 * In the initial release, this routine is only required to calculate
228 * the response to kIOReportGetDimensions, but in the future it will
229 * will enable functionality like "triggered polling" via
230 * kIOReportNotifyHubOnChange. Internally, it is already keeping
231 * track of the number of times each channel has been enabled and
232 * disabled. 13073064 tracks adding a method to see whether any
233 * channels are currently being observed.
234 *
235 * The static IOReporter::configureAllReports() will call this method
236 * on multiple reporters grouped in an OSSet.
237 *
238 * Locking: same-instance concurrency SAFE, MAY BLOCK
239 */
240 IOReturn configureReport(IOReportChannelList *channelList,
241 IOReportConfigureAction action,
242 void *result,
243 void *destination);
244
245 /*! @function IOReporter::updateReport
246 * @abstract Produce standard reply to IOService::updateReport()
247 *
248 * @param channelList - channels to update
249 * @param action - copy/trace data (see IOReportTypes.h)
250 * @param result - action-specific return value (e.g. size of data)
251 * @param destination - destination for this update (action-specific)
252 * @result appropriate IOReturn code
253 *
254 * @discussion
255 * This method searches channelList for channels tracked by this
256 * reporter, writes the corresponding data into 'destination', and
257 * updates 'result'. It should be possible to pass a given set of
258 * IOService::updateReport() arguments to any and all reporters as
259 * well as to super::updateReport() and get the right result.
260 *
261 * The static IOReporter::updateAllReports() will call this method
262 * on an OSSet of reporters.
263 *
264 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
265 */
266 IOReturn updateReport(IOReportChannelList *channelList,
267 IOReportConfigureAction action,
268 void *result,
269 void *destination);
270
271 /*! @function IOReporter::free
272 * @abstract Releases the object and all its resources.
273 *
274 * @discussion
275 * ::free() [called on last ->release()] assumes that init() [called
276 * by static ::with() methods] has completed successfully.
277 *
278 * Locking: same-instance concurrency UNSAFE
279 */
280 virtual void free(void) APPLE_KEXT_OVERRIDE;
281
282
283 /*********************************/
284 /*** 2b. Useful Static Methods ***/
285 /*********************************/
286
287 /* The following static functions are intended to simplify the management
288 * of multiple reporters. They may be superseded in the future by an
289 * IOReportManager class.
290 */
291
292 /*! @function IOReporter::configureAllReports
293 * @abstract call configureReport() on multiple IOReporter objects
294 *
295 * @param reporters - OSSet of IOReporter objects
296 * @param channelList - full list of channels to configure
297 * @param action - enable/disable/size, etc
298 * @param result - action-specific returned value
299 * @param destination - action-specific default destination
300 * @result success if all objects successfully complete
301 * IOReporter::configureReport()
302 *
303 * @discussion
304 * The OSSet must only contain IOReporter instances. The presence
305 * of non-IOReporter instances will cause this function to return
306 * kIOReturnBadArgument. If any reporter returns an error, the
307 * function will immediately return that error.
308 *
309 * Per the IOReporter::configureReport() documentation, each
310 * reporter will search channelList for channels it is reporting
311 * and provide a partial response.
312 */
313 static IOReturn configureAllReports(OSSet *reporters,
314 IOReportChannelList *channelList,
315 IOReportConfigureAction action,
316 void *result,
317 void *destination);
318 // FIXME: just put the function (inline-ish) here?
319
320 /*! @function IOReporter::updateAllReports
321 * @abstract call updateReport() on multiple IOReporter objects
322 *
323 * @param reporters - OSSet of IOReporter objects
324 * @param channelList - full list of channels to update
325 * @param action - type/style of update
326 * @param result - returned details about what was updated
327 * @param destination - destination for this update (action-specific)
328 * @result IOReturn code
329 * @discussion
330 * The OSSet must only contain IOReporter instances. The presence
331 * of non-IOReporter instances will cause this function to return
332 * kIOReturnBadArgument. If any reporter returns an error, the
333 * function will immediately return that error.
334 *
335 * Per the IOReporter::configureReport() documentation, each
336 * reporter will search channelList for channels it is reporting
337 * and provide a partial response.
338 */
339 static IOReturn updateAllReports(OSSet *reporters,
340 IOReportChannelList *channelList,
341 IOReportConfigureAction action,
342 void *result,
343 void *destination);
344 // FIXME: just put the function (inline-ish) here?
345
346
347 /* Protected (subclass-only) Methods
348 *
349 * General subclassing is not encouraged as we intend to improve
350 * internal interfaces. If you need something that might require
351 * a subclass, please file a bug against IOReporting/X and we will
352 * help you.
353 *
354 * One important concept for sub-classes (not clients) is that report
355 * data is stored in IOReportElement structures (see IOReportTypes.h).
356 */
357 protected:
358
359 /*! @function IOReporter::lockReporterConfig
360 * @function IOReporter::unlockReporterConfig
361 * @abstract prevent concurrent reconfiguration of a reporter
362 *
363 * @discussion
364 * lockReporterConfig() takes a mutex-based lock intended to prevent
365 * concurrent access to the reporter's configuration. It is not
366 * intended to prevent updates to the reporter's data. As long as
367 * all other requirements are met, it is safe to simultaneously hold
368 * both the configuration and data locks on a single reporter.
369 *
370 * lockReporterConfig() is used by routines such as addChannel().
371 * See also lockReporter() and ::handle*Swap*() below.
372 */
373 void lockReporterConfig(void);
374 void unlockReporterConfig(void);
375
376 /*! @function IOReporter::lockReporter
377 * @function IOReporter::unlockReporter
378 * @abstract prevent concurrent access to a reporter's data
379 *
380 * @discussion
381 * This method grabs a lock intended to control access the reporter's
382 * reporting data. Sub-classes maninupating internal report values
383 * must make sure the reporter is locked (usually by the most generic
384 * public interface) before calling getElementValues(),
385 * copyElementValues(), or setElementValues().
386 *
387 * Subclasses should ensure that this lock is taken exactly once
388 * before directly accessing reporter data. For example,
389 * [virtual] IOFooReporter::handleSetFoo(.) {
390 * // assert(lock_held)
391 * getElementValues(1..)
392 * getElementValues(3..)
393 * getElementValues(5..)
394 * [calculate]
395 * setElementValues(6..)
396 * }
397 * IOFooReporter::setFoo(.) { // not virtual
398 * lockReporter()
399 * handleSetFoo(.)
400 * unlockReporter()
401 * }
402 *
403 * IOReporter::handle*() use lockReporter() similarly. For example,
404 * the lock is taken by IOReporter::updateReport() and is already
405 * held by the time any ::updateChannelValues() methods are called.
406 *
407 * Subclasses cannot call this routine if the lock is already held.
408 * That's why IOReporting generally only calls it from non-virtual
409 * public methods. In particular, this method should not be called
410 * it from ::handle*() methods which exist to allow override after
411 * the lock is taken.
412 *
413 * Because lockReporter() uses a spin lock, it is SAFE to use in the
414 * interrupt context. For the same reason, however, it is UNSAFE
415 * to perform any blocking blocking operations (including memory
416 * allocations) while holding this lock.
417 */
418 void lockReporter(void);
419 void unlockReporter(void);
420
421 /*!
422 * @discussion
423 * The ::handle*Swap* functions allow subclasses to safely reconfigure
424 * their internal state. A non-virtual function handles locking
425 * and invokes the functions in order:
426 * - lockReporterConfig() // protecting instance vars but not content
427 * - prepare / allocate buffers of the new size
428 * - if error, bail (unlocking, of course)
429 *
430 * - lockReporter() // protecting data / blocking updates
431 * - swap: preserve continuing data / install new buffers
432 * - unlockReporter()
433 *
434 * - deallocate now-unused buffers
435 * - unlockReporterConfig()
436 */
437 /*! @function IOReporter::handleSwapPrepare
438 * @abstract allocate memory in preparation for an instance variable swap
439 *
440 * @param newNChannels target number of channels
441 * @result IOReturn code
442 *
443 * @discussion
444 * ::handleSwapPrepare() is responsible for allocating appropriately-
445 * sized buffers (based on the new number of channels) and storing
446 * them in _swap* instance variables. If returning and error, it
447 * must deallocate any buffers and set to NULL any _swap* variables.
448 *
449 * Locking: The caller must ensure that the *config* lock is HELD but
450 * that the reporter (data) lock is *NOT HELD*.
451 */
452 virtual IOReturn handleSwapPrepare(int newNChannels);
453
454 /*! @function IOReporter::handleAddChannelSwap
455 * @abstract update primary instance variables with new buffers
456 *
457 * @param channel_id ID of channel being added
458 * @param symChannelName optional channel name, in an allocated object
459 * @result IOReturn code
460 *
461 * @discussion
462 * handlAddChannelSwap() replaces the primary instance variables
463 * with buffers allocated in handlePrepareSwap(). It copies the the
464 * existing data into the appropriate portion of the new buffers.
465 * Because it is specific to adding one channel, it assumes that the
466 * target number of channels is one greater than the current value
467 * of _nChannels.
468 *
469 * IOReporter::handleAddChannelSwap() increments _nElements and
470 * _nChannels. To ensure that these variables describe the current
471 * buffers throughout ::handle*Swap(), subclasses overriding this
472 * method should call super::handleAddChannelSwap() after swapping
473 * their own instance variables.
474 *
475 * If returning an error, all implementations should leave their
476 * instance variables as they found them (*unswapped*). That ensures
477 * handleSwapCleanup() cleans up the unused buffers regardless of
478 * whether the swap was complete.
479 *
480 * Pseudo-code incorporating these suggestions:
481 * res = <err>; swapComplete = false;
482 * if (<unexpected>) goto finish
483 * tmpBuf = _primaryBuf; _primaryBuf = _swapBuf; _swapBuf = _primaryBuf;
484 * ...
485 * swapComplete = true;
486 * res = super::handle*Swap()
487 * ...
488 * finish:
489 * if (res && swapComplete) // unswap
490 *
491 * Locking: The caller must ensure that BOTH the configuration and
492 * reporter (data) locks are HELD.
493 */
494 virtual IOReturn handleAddChannelSwap(uint64_t channel_id,
495 const OSSymbol *symChannelName);
496
497 /*! @function IOReporter::handleSwapCleanup
498 * @abstract release and forget unused buffers
499 *
500 * @param swapNChannels channel-relative size of the _swap buffers
501 *
502 * @discussion
503 * ::handleSwapCleanup() is responsible for deallocating the buffers
504 * no longer used after a swap. It must always be called if
505 * SwapPrepare() completes successfully. Because bufers may be
506 * swapped in and out of existance, the _swap* variables may be
507 * NULL and should be set to NULL when complete.
508 *
509 * Locking: The caller must ensure that the *config* lock is HELD but
510 * that the reporter (data) lock is *NOT HELD*.
511 */
512 virtual void handleSwapCleanup(int swapNChannels);
513
514 /*! @function IOReporter::handleConfigureReport
515 * @abstract override vector for IOReporter::configureReport()
516 * [parameters and result should exactly match]
517 *
518 * @discussion
519 * The public base class method takes the reporter lock, calls this
520 * function, and then drops the lock. Subclasses should not call
521 * this function directly.
522 */
523 virtual IOReturn handleConfigureReport(IOReportChannelList *channelList,
524 IOReportConfigureAction action,
525 void *result,
526 void *destination);
527
528 /*! @function IOReporter::handleUpdateReport
529 * @abstract override vector for IOReporter::updateReport()
530 * [parameters and result should exactly match]
531 *
532 * @discussion
533 * The public base class method takes the reporter lock, calls this
534 * function, and then drops the lock. Subclasses should not call
535 * this function directly.
536 *
537 * This function may be overriden but the common case should be to
538 * simply update reporter's specific values by overriding
539 * IOReporter::updateChannelValues().
540 */
541 virtual IOReturn handleUpdateReport(IOReportChannelList *channelList,
542 IOReportConfigureAction action,
543 void *result,
544 void *destination);
545
546 /* @function IOReporter::handleCreateLegend
547 * @abstract override vector for IOReporter::createLegend()
548 * [parameters and result should exactly match]
549 *
550 * @discussion
551 * The public base class method takes the reporter lock, calls this
552 * function, and then drops the lock. Subclasses should not call
553 * this function directly.
554 */
555 virtual IOReportLegendEntry* handleCreateLegend(void);
556
557 /*! @function IOReporter::updateChannelValues
558 * @abstract update channel values for IOReporter::updateReport()
559 *
560 * @param channel_index - logical (internal) index of the channel
561 * @result appropriate IOReturn code
562 *
563 * @discussion
564 * Internal reporter method to allow a subclass to update channel
565 * data when updateReport() is called. This routine handles the
566 * common case of a subclass needing to refresh state in response
567 * to IOReporter::updateReport(). It saves the complexity of
568 * parsing the full parameters to IOReporter::updateReport().
569 *
570 * The IOReporter base class implementation does not do anything
571 * except return success.
572 *
573 * Locking: IOReporter::updateReport() takes the reporter lock,
574 * determines the indices involved, calls this function, and
575 * then proceeds to provide values to the caller. If subclasses
576 * need to call this routine directly, they must ensure that
577 * the reporter (data) lock is held: see
578 * IOReporter::lockReporter().
579 */
580 virtual IOReturn updateChannelValues(int channel_index);
581
582
583 /*! @function IOReporter::updateReportChannel
584 * @abstract Internal method to extract channel data to a destination
585 *
586 * @param channel_index - offset into internal elements array
587 * @param nElements - incremented by the number of IOReportElements added
588 * @param destination - pointer to the destination buffer
589 * @result IOReturn code
590 *
591 * @discussion
592 * updateReportChannel() is used to extract a single channel's
593 * data to the updateReport() destination.
594 *
595 * Locking: Caller must ensure that the reporter (data) lock is held.
596 */
597 IOReturn updateReportChannel(int channel_index,
598 int *nElements,
599 IOBufferMemoryDescriptor *destination);
600
601
602 /*! @function IOReporter::setElementValues
603 * @abstract Atomically update a specific member of _elements[].
604 *
605 * @param element_index - index of the _element in internal array
606 * @param values - IORepoterElementValues to replace those at _elements[idx]
607 * @param record_time - optional mach_absolute_time to be used for metadata
608 * @result IOReturn code
609 *
610 * @discussion
611 * element_index can be obtained from getFirstElementIndex(). If
612 * record_time is not provided, IOReporter::setElementValues() will
613 * fetch the current mach_absolute_time. If the current time is
614 * already known, it is more efficient to pass it along.
615 *
616 * Locking: Caller must ensure that the reporter (data) lock is held.
617 */
618 virtual IOReturn setElementValues(int element_index,
619 IOReportElementValues *values,
620 uint64_t record_time = 0);
621
622 /*! @function IOReporter::getElementValues
623 * @abstract Internal method to directly access the values of an element
624 *
625 * @param element_index - index of the _element in internal array
626 * @result A pointer to the element values requested or NULL on failure
627 *
628 * @discussion Locking: Caller must ensure that the reporter (data) lock is held.
629 * The returned pointer is only valid until unlockReporter() is called.
630 */
631 virtual const IOReportElementValues* getElementValues(int element_index);
632
633 /*! @function IOReporter::getFirstElementIndex
634 * @abstract Returns the first element index for a channel
635 *
636 * @param channel_id - ID of the channel
637 * @param element_index - pointer to the returned element_index
638 * @result appropriate IOReturn code
639 *
640 * @discussion
641 * For efficiently and thread-safely reading _elements
642 *
643 * Locking: Caller must ensure that the reporter (data) lock is held.
644 */
645 virtual IOReturn getFirstElementIndex(uint64_t channel_id,
646 int *element_index);
647
648 /*! @function IOReporter::getChannelIndex
649 * @abstract Returns the index of a channel from internal data structures
650 *
651 * @param channel_id - ID of the channel
652 * @param channel_index - pointer to the returned element_index
653 * @result appropriate IOReturn code
654 *
655 * @discussion
656 * For efficiently and thread-safely reading channels
657 *
658 * Locking: Caller must ensure that the reporter (data) lock is held.
659 */
660 virtual IOReturn getChannelIndex(uint64_t channel_id,
661 int *channel_index);
662
663 /*! @function IOReporter::getChannelIndices
664 * @abstract Returns the index of a channel and its corresponding
665 * first element index from internal data structure
666 *
667 * @param channel_id - ID of the channel
668 * @param channel_index - pointer to the returned channel_index
669 * @param element_index - pointer to the returned element_index
670 * @result appropriate IOReturn code
671 *
672 * @discussion
673 * For efficiently and thread-safely reading channel elements.
674 * It is commonly useful to get access to both channel and element
675 * indices togther. This convenience method allows sub-classes to
676 * get both indices simultaneously.
677 *
678 * Locking: Caller must ensure that the reporter (data) lock is held.
679 */
680 virtual IOReturn getChannelIndices(uint64_t channel_id,
681 int *channel_index,
682 int *element_index);
683
684 /*! @function IOReporter::copyElementValues
685 * @abstract Copies the values of an internal element to *elementValues
686 *
687 * @param element_index - Index of the element to return values from
688 * @param elementValues - For returning the content of element values
689 * @result Returns the content of an element
690 *
691 * @discussion
692 * For efficiently and thread-safely reading _elements.
693 * May need to find the index of the element first.
694 *
695 * Locking: Caller must ensure that the reporter (data) lock is held.
696 */
697 virtual IOReturn copyElementValues(int element_index,
698 IOReportElementValues *elementValues);
699
700 // private methods
701 private:
702 /*! @function IOReporter::copyChannelIDs
703 * @abstract return an an OSArray of the reporter's
704 * channel IDs
705 *
706 * @result An OSArray of the repoter's channel ID's as OSNumbers
707 *
708 * @discussion
709 * This method is an internal helper function used to prepare a
710 * legend entry. It encapsulates the channel IDs in OSNumbers and
711 * aggregates them in an OSArray used when building the IOReportLegend
712 *
713 * Locking: Caller must ensure that the reporter (data) lock is held.
714 */
715 OSArray* copyChannelIDs(void);
716
717 /*! @function IOReporter::legendWith
718 * @abstract Internal method to help create legend entries
719 *
720 * @param channelIDs - OSArray of OSNumber(uint64_t) channels IDs.
721 * @param channelNames - parrallel OSArray of OSSymbol(rich names)
722 * @param channelType - the type of all channels in this legend
723 * @param unit - The unit for the quantity recorded by this reporter object
724 *
725 * @result An IOReportLegendEntry object or NULL on failure
726 *
727 * @discussion
728 * This static method is the main legend creation function. It is called by
729 * IOReporter sub-classes and is responsible for building an
730 * IOReportLegendEntry corresponding to this reporter object.
731 * This legend entry may be extended by the sub-class of IOReporter if
732 * required.
733 *
734 * Locking: SAFE to call concurrently (no static globals), MAY BLOCK
735 */
736 static IOReportLegendEntry* legendWith(OSArray *channelIDs,
737 OSArray *channelNames,
738 IOReportChannelType channelType,
739 IOReportUnit unit);
740
741 // protected instance variables (want to get rid of these)
742 protected:
743 IOReportChannelType _channelType;
744 uint64_t _driver_id; // driver reporting data
745
746 // IOHistogramReporter accesses these; need to re-do its instantiation
747 IOReportElement *_elements;
748 int *_enableCounts; // refcount kIOReportEnable/Disable
749 uint16_t _channelDimension;// Max channel size
750 int _nElements;
751 int _nChannels; // Total Channels in this reporter
752 OSArray *_channelNames;
753
754 // MUST be protected because check is a macro!
755 bool _reporterIsLocked;
756 bool _reporterConfigIsLocked;
757
758 // Required for swapping inside addChannel
759 IOReportElement *_swapElements;
760 int *_swapEnableCounts;
761
762 // private instance variables
763 private:
764 IOReportUnit _unit;
765
766 int _enabled;// 'enabled' if _enabled > 0
767
768 IOLock *_configLock;
769 IOInterruptState _interruptState;
770 IOSimpleLock *_reporterLock;
771 };
772
773
774 /************************************/
775 /***** 3. IOReporter Subclasses *****/
776 /************************************/
777
778 /*!
779 * @class IOSimpleReporter
780 * @abstract Report simple integers
781 * @discussion
782 * Each IOSimpleReporter can have an arbitrary number of channels,
783 * each publishing a single integer value at any given time.
784 */
785
786 class IOSimpleReporter : public IOReporter
787 {
788 OSDeclareDefaultStructors(IOSimpleReporter);
789
790 public:
791
792 /*! @function IOSimpleReporter::with
793 * @abstract create an initialized simple reporter
794 *
795 * @param reportingService - IOService associated with all channels
796 * @param categories - The category in which the report should be classified
797 * @param unit - The unit for the quantity recorded by the reporter object
798 * @result On success, an instance of IOSimpleReporter, else NULL
799 *
800 * @discussion
801 * Creates an instance of IOSimpleReporter object
802 *
803 * Locking: SAFE to call concurrently (no static globals), MAY BLOCK.
804 */
805 static IOSimpleReporter* with(IOService *reportingService,
806 IOReportCategories categories,
807 IOReportUnit unit);
808
809 /*! @function IOSimpleReporter::setValue
810 * @abstract Thread safely set a channel's value
811 *
812 * @param channel_id - ID of the channel for which the value needs to be set
813 * @param value - New channel value
814 * @result Appropriate IOReturn code
815 *
816 * @discussion
817 * Updates the value of a channel to the provided value.
818 *
819 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
820 */
821 IOReturn setValue(uint64_t channel_id,
822 int64_t value);
823
824 /*! @function IOSimpleReporter::incrementValue
825 * @abstract Thread safely increment a channel's value by a given amount
826 *
827 * @param channel_id - ID of the channel for which the value needs to be incremented
828 * @param increment - Amount to be added to the current channel value
829 * @result Appropriate IOReturn code
830 * @discussion
831 * Increments the value of the channel ID by the provided amount.
832 *
833 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
834 */
835 IOReturn incrementValue(uint64_t channel_id,
836 int64_t increment);
837
838 /*! @function IOSimpleReporter::getValue
839 * @abstract Thread safely access a channel value
840 *
841 * @param channel_id - ID of the channel to get a value from
842 * @result Returns the current value stored in the channel
843 * @discussion
844 * Accessor method to a channel's current stored value
845 *
846 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
847 */
848 int64_t getValue(uint64_t channel_id);
849
850 protected:
851
852 /*! @function IOSimpleReporter::initWith
853 * @abstract instance method implementation called by IOSimpleReporter::with
854 *
855 * @discussion
856 * See description of parameters above
857 *
858 * Locking: same-instance concurrency UNSAFE
859 */
860 virtual bool initWith(IOService *reportingService,
861 IOReportCategories categories,
862 IOReportUnit unit);
863
864 private:
865 };
866
867
868
869 /*!
870 * @class IOStateReporter
871 * @abstract Report state machine data
872 * @discussion
873 * Each IOStateReporter can report information for an arbitrary number
874 * of similar state machines. All must have the same number of states.
875 */
876 class IOStateReporter : public IOReporter
877 {
878 OSDeclareDefaultStructors(IOStateReporter);
879
880 public:
881
882 /*! @function IOStateReporter::with
883 * @abstract State reporter static creation method
884 *
885 * @param reportingService - The I/O Kit service for this reporter's channels
886 * @param categories - The categories for this reporter's channels
887 * @param nstates - Maximum number of states for this reporter's channels
888 * @param unit - optional parameter if using override/increment...()
889 * @result on success, an IOStateReporter instance, else NULL
890 *
891 * @discussion
892 * Creates an instance of IOStateReporter. The default time scale
893 * is the current system's notion of mach_absolute_time(). Using a
894 * non-default time scale requires the use of
895 * override/incrementChannelState() instead of setState().
896 * setState() always updates using mach_absolute_time().
897 *
898 * Locking: SAFE to call concurrently (no static globals), MAY BLOCK
899 */
900 static IOStateReporter* with(IOService *reportingService,
901 IOReportCategories categories,
902 int nstates,
903 IOReportUnit unit = kIOReportUnitHWTicks);
904
905 /*! @function IOStateReporter::setStateID
906 * @abstract Assign a non-default ID to a state
907 *
908 * @param channel_id - ID of channel containing the state in question
909 * @param state_index - index of state to give an ID: [0..(nstates-1)]
910 * @param state_id - 64-bit state ID, for ASCII, use IOREPORT_MAKEID
911 *
912 * @result Appropriate IOReturn code
913 *
914 * @discussion
915 * By default, IOStateReporter identifies its channel states by
916 * numbering them from 0 to <nstates - 1>. If setStateID is not
917 * called to customize the state IDs, the numbered states will be
918 * kept throughout the life of the object and it is safe to reference
919 * those states by their indices. Otherwise, after setStateID() has
920 * been called, the ordering of states is no longer guaranteed and
921 * the client must reference states by their assigned state ID.
922 *
923 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
924 */
925 IOReturn setStateID(uint64_t channel_id,
926 int state_index,
927 uint64_t state_id);
928
929 /*! @function IOStateReporter::setChannelState
930 * @abstract Updates the current state of a channel to a new state
931 *
932 * @param channel_id - ID of the channel which is updated to a new state
933 * @param new_state_id - ID of the target state for this channel
934 * @param last_intransition - deprecated: time of most recent entry
935 * @param prev_state_residency - deprecated: time spent in previous state
936 * @result Appropriate IOReturn code
937 *
938 * @discussion
939 * setChannelState() updates the amount of time spent in the previous
940 * state (if any) and increments the number of transitions into the
941 * new state. It also sets the target state's last transition time to
942 * the current time and enables internal time-keeping for the channel.
943 * In this mode, calls like getStateResidencyTime() and updateReport()
944 * automatically update a channel's time in state.
945 *
946 * new_state_id identifies the target state as initialized
947 * (0..<nstates-1>) or as configured by setStateID().
948 *
949 * Drivers wishing to compute and report their own time in state
950 * should use incrementChannelState() or overrideChannelState(). It
951 * is not currently possible for a driver to synchronize with the
952 * automatic time-keeping enabled by setChannelState(). The
953 * 4-argument version of setChannelState() is thus impossible to
954 * use correctly. In the future, there may be a setChannelState()
955 * which accepts a last_intransition parameter and uses it to
956 * automatically calculate time in state (ERs -> IOReporting / X).
957 *
958 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
959 */
960 IOReturn setChannelState(uint64_t channel_id,
961 uint64_t new_state_id,
962 uint64_t last_intransition,
963 uint64_t prev_state_residency) __deprecated;
964
965 /*! @function IOStateReporter::setChannelState
966 * @abstract Updates the current state of a channel to a new state
967 *
968 * @param channel_id - ID of the channel which is updated to a new state
969 * @param new_state_id - ID of the target state for this channel
970 * @result Appropriate IOReturn code
971 *
972 * @discussion
973 * setChannelState() updates the amount of time spent in the previous
974 * state (if any) and increments the number of transitions into the
975 * new state. It also sets the target state's last transition time to
976 * the current time and enables internal time-keeping for the channel.
977 * In this mode, calls like getStateResidencyTime() and updateReport()
978 * automatically update a channel's time in state.
979 *
980 * new_state_id identifies the target state as initialized
981 * (0..<nstates-1>) or as configured by setStateID().
982 *
983 * Drivers wishing to compute and report their own time in state
984 * should use incrementChannelState() or overrideChannelState(). It
985 * is not currently possible for a driver to synchronize with the
986 * automatic time-keeping enabled by setChannelState(). The
987 * 4-argument version of setChannelState() is thus impossible to
988 * use correctly. In the future, there may be a setChannelState()
989 * which accepts a last_intransition parameter and uses it to
990 * automatically calculate time in state (ERs -> IOReporting / X).
991 *
992 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
993 */
994 IOReturn setChannelState(uint64_t channel_id,
995 uint64_t new_state_id);
996
997
998 /*! @function IOStateReporter::setState
999 * @abstract Updates state for single channel reporters
1000 *
1001 * @param new_state_id - New state for the channel
1002 * @result Appropriate IOReturn code.
1003 *
1004 * @discussion
1005 * setState() is a convenience method for single-channel state
1006 * reporter instances. An error will be returned if the reporter
1007 * in question has more than one channel.
1008 *
1009 * See further discussion at setChannelState().
1010 *
1011 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1012 */
1013 IOReturn setState(uint64_t new_state_id);
1014
1015 /*! @function IOStateReporter::setState
1016 * @abstract Updates state for single channel reporters
1017 *
1018 * @param new_state_id - New state for the channel
1019 * @param last_intransition - deprecated: time of most recent entry
1020 * @param prev_state_residency - deprecated: spent in previous state
1021 * @result Appropriate IOReturn code.
1022 *
1023 * @discussion
1024 * setState() is a convenience method for single-channel state
1025 * reporter instances. An error will be returned if the reporter
1026 * in question has more than one channel.
1027 *
1028 * See further discussion at setChannelState().
1029 *
1030 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1031 */
1032 IOReturn setState(uint64_t new_state_id,
1033 uint64_t last_intransition,
1034 uint64_t prev_state_residency) __deprecated;
1035
1036 /*! @function IOStateReporter::overrideChannelState
1037 * @abstract Overrides state data for a channel with passed arguments
1038 *
1039 * @param channel_id - ID of the channel which state is to be updated
1040 * @param state_id - state id for the channel
1041 * @param time_in_state - time used as new total time in state
1042 * @param intransitions - total number of transitions into state
1043 * @param last_intransition - mach_absolute_time of most recent entry (opt)
1044 * @result Appropriate IOReturn code
1045 *
1046 * @discussion
1047 * overrideChannelState() sets a particular state's time in state
1048 * and transition count to the values provided. The optional
1049 * last_intransition records the last time the channel transitioned
1050 * into the given state. Passing 0 for time_in_state and
1051 * intransitions will force the current values to 0. Passing 0
1052 * for last_intransition for all states will disable the notion
1053 * of a channel's "current state."
1054 *
1055 * The most recent last_intransition (amongst all states in a channel)
1056 * logically determines the current state. If last_intransition is
1057 * not provided for any state, the channel will not report a current
1058 * For consistent results, it is important to either never specify
1059 * last_intransition or to always specify it.
1060 *
1061 * There is currently a bug in determining current state (13423273).
1062 * The IOReportMacros.h macros only update the state's metadata
1063 * timestamp and libIOReport only looks at the metadata timestamps
1064 * to determine the current state. Until that bug is fixed, whichever
1065 * state is updated most recently will be considered the "current"
1066 * state by libIOReport.
1067 *
1068 * ::setState()'s automatic "time in state" updates are not supported
1069 * when using overrideChannelState(). Clients must not use
1070 * overrideChannelState() on any channel that has ::setState() called
1071 * on it. Unlike with ::setState(), clients using
1072 * overrideChannelState() are responsible for ensuring that data is
1073 * up to date for updateReport() calls. The correct way to do this
1074 * is for a driver's ::updateReport() method to push the most up to
1075 * date values into the reporters before calling
1076 * super::updateReport().
1077 *
1078 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1079 */
1080 IOReturn overrideChannelState(uint64_t channel_id,
1081 uint64_t state_id,
1082 uint64_t time_in_state,
1083 uint64_t intransitions,
1084 uint64_t last_intransition = 0);
1085
1086 /*! @function IOStateReporter::incrementChannelState
1087 * @abstract Updates state data for a channel with passed arguments
1088 *
1089 * @param channel_id - ID of the channel which state is to be updated
1090 * @param state_id - state id for the channel
1091 * @param time_in_state - time to be accumulated for time in state
1092 * @param intransitions - number of transitions into state to be added
1093 * @param last_intransition - mach_absolute_time of most recent entry (opt)
1094 * @result Appropriate IOReturn code
1095 *
1096 * @discussion
1097 * incrementChannelState() adds time_in_state and intransitions
1098 * to the current values stored for a particular state. If provided,
1099 * last_intransition overwrites the time the state was most recently
1100 * entered. Passing 0 for time_in_state and intransitions will have
1101 * no effect. Passing 0 for last_intransition for all states will
1102 * disable the notion of a channel's "current state."
1103 *
1104 * The most recent last_intransition (amongst all states in a channel)
1105 * logically determines the current state. If last_intransition is
1106 * not provided for any state, the channel will not report a current
1107 * For consistent results, it is important to either never specify
1108 * last_intransition or to always specify it.
1109 *
1110 * There is currently a bug in determining current state (13423273).
1111 * The IOReportMacros.h macros only update the state's metadata
1112 * timestamp and libIOReport only looks at the metadata timestamps
1113 * to determine the current state. Until that bug is fixed, whichever
1114 * state is updated most recently will be considered the "current"
1115 * state by libIOReport.
1116 *
1117 * ::setState()'s automatic "time in state" updates are not supported
1118 * when using incrementChannelState(). Clients must not use
1119 * incrementChannelState() on any channel that has ::setState()
1120 * called on it. Unlike with ::setState(), clients using
1121 * incrementChannelState() are responsible for ensuring that data
1122 * is up to date for updateReport() calls. The correct way to do
1123 * this is for a driver's ::updateReport() method to push the most
1124 * up to date values into the reporters before calling
1125 * super::updateReport().
1126 *
1127 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1128 */
1129 IOReturn incrementChannelState(uint64_t channel_id,
1130 uint64_t state_id,
1131 uint64_t time_in_state,
1132 uint64_t intransitions,
1133 uint64_t last_intransition = 0);
1134
1135 /*! @function IOStateReporter::setStateByIndices
1136 * @abstract update a channel state without validating channel_id
1137 *
1138 * @param channel_index - 0..<nChannels>, available from getChannelIndex()
1139 * @param new_state_index - New state (by index) for the channel
1140 * @result Appropriate IOReturn code
1141 *
1142 * @discussion
1143 * Similar to setState(), setStateByIndices() sets a channel's state
1144 * without searching for the channel or state IDs. It will perform
1145 * bounds checking, but relies on the caller to properly indicate
1146 * the indices of the channel and state. Clients can rely on channels
1147 * being added to IOStateReporter in order: the first channel will
1148 * have index 0, the second index 1, etc. Like ::setState(),
1149 * "time in state" calculations are handled automatically.
1150 *
1151 * setStateByIndices() is faster than than setChannelState(), but
1152 * it should only be used where the latter's performance overhead
1153 * might be a problem. For example, many channels in a single
1154 * reporter and high-frequency state changes.
1155 *
1156 * Drivers wishing to compute and report their own time in state
1157 * should use incrementChannelState() or overrideChannelState(). It
1158 * is not currently possible for a driver to synchronize with the
1159 * automatic time-keeping enabled by setStateByIndices(). The
1160 * 4-argument version of setChannelState() is thus impossible to
1161 * use correctly. In the future, there may be a setChannelState()
1162 * which accepts a last_intransition parameter and uses it to
1163 * automatically calculate time in state (ERs -> IOReporting / X).
1164 *
1165 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1166 */
1167 IOReturn setStateByIndices(int channel_index,
1168 int new_state_index);
1169
1170 /*! @function IOStateReporter::setStateByIndices
1171 * @abstract update a channel state without validating channel_id
1172 *
1173 * @param channel_index - 0..<nChannels>, available from getChannelIndex()
1174 * @param new_state_index - New state (by index) for the channel
1175 * @param last_intransition - deprecated: time of most recent entry
1176 * @param prev_state_residency - deprecated: time spent in previous state
1177 * @result Appropriate IOReturn code
1178 *
1179 * @discussion
1180 * Similar to setState(), setStateByIndices() sets a channel's state
1181 * without searching for the channel or state IDs. It will perform
1182 * bounds checking, but relies on the caller to properly indicate
1183 * the indices of the channel and state. Clients can rely on channels
1184 * being added to IOStateReporter in order: the first channel will
1185 * have index 0, the second index 1, etc. Like ::setState(),
1186 * "time in state" calculations are handled automatically.
1187 *
1188 * setStateByIndices() is faster than than setChannelState(), but
1189 * it should only be used where the latter's performance overhead
1190 * might be a problem. For example, many channels in a single
1191 * reporter and high-frequency state changes.
1192 *
1193 * Drivers wishing to compute and report their own time in state
1194 * should use incrementChannelState() or overrideChannelState(). It
1195 * is not currently possible for a driver to synchronize with the
1196 * automatic time-keeping enabled by setStateByIndices(). The
1197 * 4-argument version of setChannelState() is thus impossible to
1198 * use correctly. In the future, there may be a setChannelState()
1199 * which accepts a last_intransition parameter and uses it to
1200 * automatically calculate time in state (ERs -> IOReporting / X).
1201 *
1202 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1203 */
1204 IOReturn setStateByIndices(int channel_index,
1205 int new_state_index,
1206 uint64_t last_intransition,
1207 uint64_t prev_state_residency) __deprecated;
1208
1209 /*! @function IOStateReporter::getStateInTransitions
1210 * @abstract Accessor method for count of transitions into state
1211 *
1212 * @param channel_id - ID of the channel
1213 * @param state_id - State of the channel
1214 * @result Count of transitions into the requested state.
1215 *
1216 * @discussion
1217 * Some clients may need to consume internally the data aggregated by the
1218 * reporter object. This method allows a client to retrieve the count of
1219 * transitions into the requested state for the channel_id.
1220 *
1221 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1222 */
1223 uint64_t getStateInTransitions(uint64_t channel_id,
1224 uint64_t state_id);
1225
1226 /*! @function IOStateReporter::getStateResidencyTime
1227 * @abstract Accessor method for time spent in a given state
1228 *
1229 * @param channel_id - ID of the channel
1230 * @param state_id - State of the channel
1231 * @result Absolute time spent in specified state
1232 *
1233 * @discussion
1234 * Some clients may need to consume internally the data aggregated
1235 * by the by the reporter object. This method allows a client to
1236 * retrieve the absolute time a particular channel recorded as spent
1237 * in a specified state.
1238 *
1239 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1240 */
1241 uint64_t getStateResidencyTime(uint64_t channel_id,
1242 uint64_t state_id);
1243
1244 /*! @function IOStateReporter::getStateLastTransitionTime
1245 * @abstract Accessor method for last time a transition occured
1246 *
1247 * @param channel_id - ID of the channel
1248 * @param state_id - State of the channel
1249 * @result Absolute time for when the last transition occured
1250 *
1251 * @discussion
1252 * Some clients may need to consume internally the data aggregated
1253 * by the by the reporter object. This method allows a client to
1254 * retrieve the absolute time stamp for when the last transition into
1255 * a specific state was recorded.
1256 *
1257 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1258 */
1259 uint64_t getStateLastTransitionTime(uint64_t channel_id, uint64_t state_id);
1260
1261 /*! @function [DEPRECATED] IOStateReporter::getStateLastChannelUpdateTime
1262 * @abstract Deprecated accessor for last time a channel was auto-updated
1263 *
1264 * @param channel_id - ID of the channel
1265 * @result Absolute time for last time the channel was updated
1266 *
1267 * @discussion
1268 * If a channel has had ::setState() called on it, calls such as
1269 * getStateResidencyTime() or updateReport() will update time in the
1270 * current state and update an internal "last channel update time."
1271 * Because clients have no way to interlock with those methods, there
1272 * is no sensible way to use this method and it will be removed in
1273 * a future release.
1274 *
1275 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1276 */
1277 uint64_t getStateLastChannelUpdateTime(uint64_t channel_id) __deprecated;
1278
1279 /*! @function IOStateReporter::free
1280 * @abstract Releases the object and all its resources.
1281 *
1282 * @discussion
1283 * ::free() assumes that init() has completed. Clients should use
1284 * the static ::with() methods to obtain fully-initialized reporter
1285 * instances.
1286 *
1287 * Locking: same-instance concurrency UNSAFE
1288 */
1289 virtual void free(void) APPLE_KEXT_OVERRIDE;
1290
1291 protected:
1292
1293 /*! @function IOStateReporter::initWith
1294 * @abstract Instance method implementation called by ::with
1295 *
1296 * @discussion
1297 * See description of parameters above
1298 */
1299 virtual bool initWith(IOService *reportingService,
1300 IOReportCategories categories,
1301 int16_t nstates, IOReportUnit unit);
1302
1303
1304 /*! @function IOStateReporter::handleSwapPrepare
1305 * @abstract _swap* = <IOStateReporter-specific per-channel buffers>
1306 * [see IOReporter::handle*Swap* for more info]
1307 */
1308 virtual IOReturn handleSwapPrepare(int newNChannels) APPLE_KEXT_OVERRIDE;
1309
1310 /*!
1311 * @function IOStateReporter::handleAddChannelSwap
1312 * @abstract swap in IOStateReporter's variables
1313 */
1314 virtual IOReturn handleAddChannelSwap(uint64_t channel_id,
1315 const OSSymbol *symChannelName) APPLE_KEXT_OVERRIDE;
1316
1317 /*!
1318 * @function IOStateReporter::handleSwapCleanup
1319 * @abstract clean up unused buffers in _swap*
1320 */
1321 virtual void handleSwapCleanup(int swapNChannels) APPLE_KEXT_OVERRIDE;
1322
1323 /*! @function IOStateReporter::updateChannelValues
1324 * @abstract Update accounting of time spent in current state
1325 *
1326 * @param channel_index - internal index of the channel
1327 * @result appropriate IOReturn code
1328 *
1329 * @discussion
1330 * Internal State reporter method to account for the time spent in
1331 * the current state when updateReport() is called on the reporter's
1332 * channels.
1333 *
1334 * Locking: Caller must ensure that the reporter (data) lock is held.
1335 */
1336 virtual IOReturn updateChannelValues(int channel_index) APPLE_KEXT_OVERRIDE;
1337
1338 /*! @function IOStateReporter::setStateByIndices
1339 * @abstract update a channel state without validating channel_id
1340 *
1341 * @param channel_index - 0..<nChannels>, available from getChannelIndex()
1342 * @param new_state_index - New state for the channel
1343 * @param last_intransition - to remove: time of most recent entry
1344 * @param prev_state_residency - to remove: time spent in previous state
1345 * @result Appropriate IOReturn code
1346 *
1347 * @discussion
1348 * Locked version of IOReporter::setStateByIndices(). This method may be
1349 * overriden by sub-classes.
1350 *
1351 * Locking: Caller must ensure that the reporter (data) lock is held.
1352 */
1353 virtual IOReturn handleSetStateByIndices(int channel_index,
1354 int new_state_index,
1355 uint64_t last_intransition,
1356 uint64_t prev_state_residency);
1357
1358 /*! @function IOStateReporter::setStateID
1359 * @abstract Assign a non-default ID to a state
1360 *
1361 * @param channel_id - ID of channel containing the state in question
1362 * @param state_index - index of state to give an ID: [0..(nstates-1)]
1363 * @param state_id - 64-bit state ID, for ASCII, use IOREPORT_MAKEID
1364 *
1365 * @result Appropriate IOReturn code
1366 *
1367 * @discussion
1368 * Locked version of IOReporter::setStateID(). This method may be
1369 * overriden by sub-classes
1370 *
1371 * Locking: Caller must ensure that the reporter (data) lock is held.
1372 */
1373 virtual IOReturn handleSetStateID(uint64_t channel_id,
1374 int state_index,
1375 uint64_t state_id);
1376
1377 /*! @function IOStateReporter::handleOverrideChannelStateByIndices
1378 * @abstract Overrides state data for a channel with passed arguments
1379 *
1380 * @param channel_index - index of the channel which state is to be updated
1381 * @param state_index - index of the state id for the channel
1382 * @param time_in_state - time used as new total time in state
1383 * @param intransitions - total number of transitions into state
1384 * @param last_intransition - mach_absolute_time of most recent entry (opt)
1385 * @result Appropriate IOReturn code
1386 *
1387 * @discussion
1388 * Locked version of IOReporter::overrideChannelState(). This method
1389 * may be overriden by sub-classes.
1390 *
1391 * Locking: Caller must ensure that the reporter (data) lock is held.
1392 */
1393 virtual IOReturn handleOverrideChannelStateByIndices(int channel_index,
1394 int state_index,
1395 uint64_t time_in_state,
1396 uint64_t intransitions,
1397 uint64_t last_intransition = 0);
1398
1399 /*! @function IOStateReporter::handleIncrementChannelStateByIndices
1400 * @abstract Updates state data for a channel with passed arguments
1401 *
1402 * @param channel_index - index of the channel which state is to be updated
1403 * @param state_index - index of the state id for the channel
1404 * @param time_in_state - time used as new total time in state
1405 * @param intransitions - total number of transitions into state
1406 * @param last_intransition - mach_absolute_time of most recent entry (opt)
1407 * @result Appropriate IOReturn code
1408 *
1409 * @discussion
1410 * Locked version of IOReporter::incrementChannelState(). This method
1411 * may be overriden by sub-classes.
1412 *
1413 * Locking: Caller must ensure that the reporter (data) lock is held.
1414 */
1415 virtual IOReturn handleIncrementChannelStateByIndices(int channel_index,
1416 int state_index,
1417 uint64_t time_in_state,
1418 uint64_t intransitions,
1419 uint64_t last_intransition = 0);
1420 private:
1421
1422 int *_currentStates; // current states (per chonnel)
1423 uint64_t *_lastUpdateTimes; // most recent auto-update
1424
1425 // Required for swapping inside addChannel
1426 int *_swapCurrentStates;
1427 uint64_t *_swapLastUpdateTimes;
1428
1429 enum valueSelector {
1430 kInTransitions,
1431 kResidencyTime,
1432 kLastTransitionTime
1433 };
1434 uint64_t _getStateValue(uint64_t channel_id,
1435 uint64_t state_id,
1436 enum valueSelector value);
1437
1438 IOReturn _getStateIndices(uint64_t channel_id,
1439 uint64_t state_id,
1440 int *channel_index,
1441 int *state_index);
1442 };
1443
1444
1445 /*!
1446 * @class IOHistogramReporter
1447 * @abstract Report histograms of values
1448 * @discussion
1449 * Each IOHistogramReporter can report one histogram representing
1450 * how a given value has changed over time.
1451 */
1452 class IOHistogramReporter : public IOReporter
1453 {
1454 OSDeclareDefaultStructors(IOHistogramReporter);
1455
1456 public:
1457 /*! @function IOHistogramReporter::with
1458 * @abstract Initializes the IOHistogramReporter instance variables and data structures
1459 *
1460 * @param reportingService - The I/O Kit service for this reporter's channels
1461 * @param categories - The categories in which the report should be classified
1462 * @param channelID - uint64_t channel identifier
1463 * @param channelName - rich channel name as char*
1464 * @param unit - The unit for the quantity recorded by the reporter object
1465 * @param nSegments - Number of segments to be extracted from the config data structure
1466 * @param config - Histograms require the caller to pass a configuration by segments
1467 * @result an instance of the IOSimpleReporter object or NULL on error
1468 *
1469 * @discussion
1470 * Creates an instance of histogram reporter object.
1471 *
1472 * FIXME: need more explanation of the config
1473 *
1474 * IOHistogramReporter currently only supports a single channel.
1475 *
1476 *
1477 */
1478 static IOHistogramReporter* with(IOService *reportingService,
1479 IOReportCategories categories,
1480 uint64_t channelID,
1481 const char *channelName,
1482 IOReportUnit unit,
1483 int nSegments,
1484 IOHistogramSegmentConfig *config);
1485
1486 /*! @function IOHistogramReporter::addChannel
1487 * @abstract Override IOReporter::addChannel(*) to return an error
1488 *
1489 * @result kIOReturnUnsupported - doesn't support adding channels
1490 */
1491 IOReturn
1492 addChannel(__unused uint64_t channelID, __unused const char *channelName = NULL)
1493 {
1494 return kIOReturnUnsupported;
1495 }
1496
1497 /*! @function IOHistogramReporter::overrideBucketValues
1498 * @abstract Override values of a bucket at specified index
1499 *
1500 * @param index - index of bucket to override
1501 * @param bucket_hits - new bucket hits count
1502 * @param bucket_min - new bucket minimum value
1503 * @param bucket_max - new bucket maximum value
1504 * @param bucket_sum - new bucket sum
1505 * @result Appropriate IOReturn code
1506 *
1507 * @discussion
1508 * Replaces data in the bucket at the specified index with the data pointed
1509 * to by bucket. No sanity check is performed on the data. If the index
1510 * is out of bounds, kIOReturnBadArgument is returned.
1511 *
1512 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1513 */
1514
1515 IOReturn overrideBucketValues(unsigned int index,
1516 uint64_t bucket_hits,
1517 int64_t bucket_min,
1518 int64_t bucket_max,
1519 int64_t bucket_sum);
1520
1521 /*! @function IOHistogramReporter::tallyValue
1522 * @abstract Add a new value to the histogram
1523 *
1524 * @param value - new value to add to the histogram
1525 * @result the index of the affected bucket, or -1 on error
1526 *
1527 * @discussion
1528 * The histogram reporter determines in which bucket the value
1529 * falls and increments it. The lowest and highest buckets
1530 * extend to negative and positive infinity, respectively.
1531 *
1532 * Locking: same-instance concurrency SAFE, WILL NOT BLOCK
1533 */
1534 int tallyValue(int64_t value);
1535
1536 /*! @function IOHistogramReporter::free
1537 * @abstract Releases the object and all its resources.
1538 *
1539 * @discussion
1540 * ::free() assumes that init() has completed. Clients should use
1541 * the static ::with() methods to obtain fully-initialized reporter
1542 * instances.
1543 *
1544 * Locking: same-instance concurrency UNSAFE
1545 */
1546 virtual void free(void) APPLE_KEXT_OVERRIDE;
1547
1548 protected:
1549
1550 /*! @function IOHistogramReporter::initWith
1551 * @abstract instance method implementation called by ::with
1552 *
1553 * @discussion
1554 * See description of parameters above
1555 */
1556 virtual bool initWith(IOService *reportingService,
1557 IOReportCategories categories,
1558 uint64_t channelID,
1559 const OSSymbol *channelName,
1560 IOReportUnit unit,
1561 int nSegments,
1562 IOHistogramSegmentConfig *config);
1563
1564 /*! @function IOHistogramReporter::handleCreateLegend
1565 * @abstract Builds an IOReporting legend entry representing the channels of this reporter.
1566 *
1567 * @result An IOReportLegendEntry or NULL on failure
1568 *
1569 * @discussion
1570 * The returned legend entry may be appended to kIOReportLegendKey
1571 * to be published by the caller in the IORegistry. See the
1572 * IOReportLegend class for more details.
1573 *
1574 * Locking: same-instance concurrency SAFE, MAY BLOCK
1575 */
1576 IOReportLegendEntry* handleCreateLegend(void) APPLE_KEXT_OVERRIDE;
1577
1578
1579 private:
1580
1581 int _segmentCount;
1582 int64_t *_bucketBounds;
1583 int _bucketCount;
1584 IOHistogramSegmentConfig *_histogramSegmentsConfig;
1585 };
1586
1587
1588 /***********************************/
1589 /***** 4. IOReportLegend Class *****/
1590 /***********************************/
1591
1592 /*!
1593 * @class IOReportLegend
1594 * @abstract combine legend entries into a complete legend
1595 * @discussion
1596 * IOReportLegend adds metadata to legend entries and combines them
1597 * into a single OSArray that can be published under the
1598 * kIOReportLegendKey property in the I/O Kit registry.
1599 */
1600 class IOReportLegend : public OSObject
1601 {
1602 OSDeclareDefaultStructors(IOReportLegend);
1603
1604 public:
1605 /*! @function IOReportLegend::with
1606 * @abstract Create an instance of IOReportLegend
1607 *
1608 * @param legend - OSArray of the legend possibly already present in registry
1609 * @result an instance of IOReportLegend, or NULL on failure
1610 *
1611 * @discussion
1612 * An IOReporting legend (an OSArray of legend entries) may be already
1613 * present in the IORegistry. Thus the recommended way to publish
1614 * new entries is to append to any existing array as follows:
1615 * 1. call getProperty(kIOReportLegendKey) to get an existing legend.
1616 *
1617 * 2a. If it exists
1618 * - OSDynamicCast to OSArray
1619 * - and pass it to ::with()
1620 * IOReportLegend *legendMaker = IOReportLegend::with(legend);
1621 * The provided array is retained by IOReportLegend.
1622 *
1623 * 2b. If no legend already exists in the registry, pass NULL
1624 * IOReportLegend *legend = IOReportLegend::with(NULL);
1625 * This latter invocation will cause IOReportLegend to create a new
1626 * array internally (also holding one reference).
1627 *
1628 * At the cost of some registry churn, the static
1629 * IOReportLegend::addReporterLegend() will handle the above, removing
1630 * the need for any direct use of the IOReportLegend class.
1631 */
1632 static IOReportLegend* with(OSArray *legend);
1633
1634 /*! @function IOReportLegend::addLegendEntry
1635 * @abstract Add a new legend entry
1636 *
1637 * @param legendEntry - entry to be added to the internal legend array
1638 * @param groupName - primary group name for this entry
1639 * @param subGroupName - secondary group name for this entry
1640 * @result appropriate IOReturn code
1641 *
1642 * @discussion
1643 * The entry will be retained as an element of the internal array.
1644 * Legend entries are available from reporter objects. Entries
1645 * represent some number of channels with similar properties (such
1646 * as group and sub-group). Multiple legend entries with the same
1647 * group names will be aggregated in user space.
1648 *
1649 * Drivers that instantiate their reporter objects in response to
1650 * IOService::configureReport(kIOReportDisable) will need to create
1651 * temporary reporter objects for the purpose of creating their
1652 * legend entries. User-space legends are tracked by 12836893.
1653 */
1654 IOReturn addLegendEntry(IOReportLegendEntry *legendEntry,
1655 const char *groupName,
1656 const char *subGroupName);
1657
1658 /*! @function IOReportLegend::addReporterLegend
1659 * @abstract Add a legend entry from a reporter object
1660 *
1661 * @param reporter - IOReporter to use to extract and append the legend
1662 * @param groupName - primary group name for this entry
1663 * @param subGroupName - secondary group name for this entry
1664 * @result appropriate IOReturn code
1665 *
1666 * @discussion
1667 * An IOReportLegendEntry will be created internally to this method from
1668 * the IOReporter object passed in argument. The entry will be released
1669 * internally after being appended to the IOReportLegend object.
1670 * Legend entries are available from reporter objects. Entries
1671 * represent some number of channels with similar properties (such
1672 * as group and sub-group). Multiple legend entries with the same
1673 * group names will be aggregated in user space.
1674 *
1675 * Drivers that instantiate their reporter objects in response to
1676 * IOService::configureReport(kIOReportDisable) will need to create
1677 * temporary reporter objects for the purpose of creating their
1678 * legend entries. User-space legends are tracked by 12836893.
1679 *
1680 * Locking: same-reportingService and same-IORLegend concurrency UNSAFE
1681 */
1682 IOReturn addReporterLegend(IOReporter *reporter,
1683 const char *groupName,
1684 const char *subGroupName);
1685
1686 /*! @function IOReportLegend::addReporterLegend
1687 * @abstract Add a legend entry from a reporter object
1688 *
1689 * @param reportingService - IOService data provider into the reporter object
1690 * @param reporter - IOReporter to use to extract and append the legend
1691 * @param groupName - primary group name for this entry
1692 * @param subGroupName - secondary group name for this entry
1693 * @result appropriate IOReturn code
1694 *
1695 * @discussion
1696 * An IOReportLegendEntry will be created internally to this method from
1697 * the IOReporter object passed in argument. The entry will be released
1698 * internally after being appended to the IOReportLegend object.
1699 * Legend entries are available from reporter objects. Entries
1700 * represent some number of channels with similar properties (such
1701 * as group and sub-group). Multiple legend entries with the same
1702 * group names will be aggregated in user space.
1703 *
1704 * Drivers that instantiate their reporter objects in response to
1705 * IOService::configureReport(kIOReportDisable) will need to create
1706 * temporary reporter objects for the purpose of creating their
1707 * legend entries. User-space legends are tracked by 12836893.
1708 *
1709 * The static version of addReporterLegend adds the reporter's legend
1710 * directly to reportingService's kIOReportLegendKey. It is not
1711 * possible to safely update kIOReportLegendKey from multiple threads.
1712 *
1713 * Locking: same-reportingService and same-IORLegend concurrency UNSAFE
1714 */
1715 static IOReturn addReporterLegend(IOService *reportingService,
1716 IOReporter *reporter,
1717 const char *groupName,
1718 const char *subGroupName);
1719
1720 /*! @function IOReportLegend::getLegend
1721 * @abstract Accessor method to get the legend array
1722 *
1723 * @result Returns the OSObject holding the legend to be published by the driver
1724 * @discussion
1725 * This array will include all legend entries added to the object.
1726 */
1727 OSArray* getLegend(void);
1728
1729 /*! @function IOReportLegend::free
1730 * @abstract Frees the IOReportLegend object
1731 *
1732 * @discussion
1733 * ::free() cleans up the reporter and anything it allocated.
1734 *
1735 * ::free() releases the internal array (which was either passed
1736 * to ::with() or created as a result of ::with(NULL)). Assuming
1737 * the caller extracted the array with getLegend() and published it
1738 * in the I/O Kit registry, its ownership will now be with the
1739 * registry.
1740 */
1741 void free(void) APPLE_KEXT_OVERRIDE;
1742
1743
1744
1745 protected:
1746
1747 private:
1748
1749 OSArray *_reportLegend;
1750
1751 IOReturn initWith(OSArray *legend);
1752
1753 /*! @function IOReportLegend::organizeLegend
1754 * @abstract Sets up the legend entry, organizing it with group and sub-group names
1755 *
1756 * @param groupName - Primary group name
1757 * @param subGroupName - Secondary group name
1758 * @result IOReturn code
1759 */
1760 IOReturn organizeLegend(IOReportLegendEntry *legendEntry,
1761 const OSSymbol *groupName,
1762 const OSSymbol *subGroupName);
1763
1764 // FUTURE POSSIBILITY (NOT IMPLEMENTED!)
1765 /*! @function IOReportLegend::createReporters
1766 * @abstract Creates as many IOReporter objects as the legend contains
1767 *
1768 * @param legend - OSArray legend object containing the description of all reporters
1769 * the driver is able to address
1770 * @param reporter - OSSet of reporter objects created by this call
1771 * @result IOReturn code kIOReturnSuccess if successful
1772 *
1773 * @discussion
1774 * NOT SUPPORTED at the time of writing
1775 * Convenience method to create all the driver's reporter objects from a legend.
1776 * Can be used when a legend is made public through the IORegistry but IOReporter
1777 * objects have not yet been created to save memory, waiting for observers.
1778 * Upon a call to configureReport via the IOService method, a driver could
1779 * create all reporter objects on the fly using this function.
1780 */
1781 // For Future IOReporterManager...
1782 // static IOReturn createReporters(requestedChannels, legend);
1783 };
1784
1785 #endif /* ! _IOKERNEL_REPORTERS_H_ */