2005-10-28 16:38:24 +00:00
|
|
|
|
/* -*-objc-*- */
|
|
|
|
|
|
|
|
|
|
/** Implementation of GSThroughput for GNUStep
|
|
|
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
Date: October 2005
|
|
|
|
|
|
|
|
|
|
This file is part of the Performance Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
|
|
$Date$ $Revision$
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
|
#include <Foundation/NSData.h>
|
|
|
|
|
#include <Foundation/NSDate.h>
|
2005-10-29 03:01:50 +00:00
|
|
|
|
#include <Foundation/NSCalendarDate.h>
|
2005-10-28 16:38:24 +00:00
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
|
#include <Foundation/NSHashTable.h>
|
|
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
|
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
|
#include <Foundation/NSTimer.h>
|
2005-10-29 16:07:38 +00:00
|
|
|
|
#include <Foundation/NSThread.h>
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
|
|
|
|
#include "GSThroughput.h"
|
2005-11-14 18:14:21 +00:00
|
|
|
|
#include "GSTicker.h"
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
|
|
|
|
#define MAXDURATION 24.0*60.0*60.0
|
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
@class GSThroughputThread;
|
|
|
|
|
|
2005-10-28 16:38:24 +00:00
|
|
|
|
static Class NSDateClass = 0;
|
|
|
|
|
static SEL tiSel = 0;
|
|
|
|
|
static NSTimeInterval (*tiImp)(Class,SEL) = 0;
|
|
|
|
|
|
2005-10-29 09:23:09 +00:00
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned cnt; // Number of events.
|
|
|
|
|
unsigned tick; // Start time
|
|
|
|
|
} CInfo;
|
|
|
|
|
|
2005-10-28 16:38:24 +00:00
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned cnt; // Number of events.
|
|
|
|
|
NSTimeInterval max; // Longest duration
|
|
|
|
|
NSTimeInterval min; // Shortest duration
|
|
|
|
|
NSTimeInterval sum; // Total (sum of durations for event)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
unsigned tick; // Start time
|
|
|
|
|
} DInfo;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
|
|
|
|
typedef struct {
|
2005-10-29 09:23:09 +00:00
|
|
|
|
void *seconds;
|
|
|
|
|
void *minutes;
|
|
|
|
|
void *periods;
|
|
|
|
|
BOOL supportDurations;
|
|
|
|
|
unsigned numberOfPeriods;
|
|
|
|
|
unsigned minutesPerPeriod;
|
|
|
|
|
unsigned second;
|
|
|
|
|
unsigned minute;
|
|
|
|
|
unsigned period;
|
|
|
|
|
unsigned last; // last tick used
|
|
|
|
|
NSTimeInterval started; // When duration logging started.
|
2005-10-29 16:07:38 +00:00
|
|
|
|
NSString *event; // Name of current event
|
|
|
|
|
NSString *name; // Name of this instance
|
|
|
|
|
GSThroughputThread *thread; // Thread info
|
2005-10-28 16:38:24 +00:00
|
|
|
|
} Item;
|
2005-11-04 06:00:41 +00:00
|
|
|
|
#define my ((Item*)_data)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
2005-10-29 09:23:09 +00:00
|
|
|
|
#define cseconds ((CInfo*)my->seconds)
|
|
|
|
|
#define cminutes ((CInfo*)my->minutes)
|
|
|
|
|
#define cperiods ((CInfo*)my->periods)
|
|
|
|
|
#define dseconds ((DInfo*)my->seconds)
|
|
|
|
|
#define dminutes ((DInfo*)my->minutes)
|
|
|
|
|
#define dperiods ((DInfo*)my->periods)
|
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@interface GSThroughputThread : NSObject
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
@public
|
|
|
|
|
NSTimer *theTimer;
|
|
|
|
|
NSHashTable *instances;
|
|
|
|
|
}
|
|
|
|
|
@end
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
@interface GSThroughput (Private)
|
|
|
|
|
+ (GSThroughputThread*) _threadInfo;
|
2005-11-14 18:14:21 +00:00
|
|
|
|
+ (void) newSecond: (GSThroughputThread*)t;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
- (void) _detach;
|
|
|
|
|
- (void) _update;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSThroughputThread
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (instances != 0)
|
|
|
|
|
{
|
|
|
|
|
NSHashEnumerator e;
|
|
|
|
|
GSThroughput *i;
|
|
|
|
|
|
|
|
|
|
e = NSEnumerateHashTable(instances);
|
|
|
|
|
while ((i = (GSThroughput*)NSNextHashEnumeratorItem(&e)) != nil)
|
|
|
|
|
{
|
|
|
|
|
[i _detach];
|
|
|
|
|
}
|
|
|
|
|
NSEndHashTableEnumeration(&e);
|
|
|
|
|
NSFreeHashTable(instances);
|
|
|
|
|
instances = 0;
|
|
|
|
|
}
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
instances = NSCreateHashTable(NSNonRetainedObjectHashCallBacks, 0);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSThroughput (Private)
|
|
|
|
|
|
|
|
|
|
+ (GSThroughputThread*) _threadInfo
|
|
|
|
|
{
|
|
|
|
|
GSThroughputThread *t;
|
|
|
|
|
|
|
|
|
|
t = [[[NSThread currentThread] threadDictionary]
|
|
|
|
|
objectForKey: @"GSThroughput"];
|
|
|
|
|
if (t == nil)
|
|
|
|
|
{
|
|
|
|
|
t = [GSThroughputThread new];
|
|
|
|
|
[[[NSThread currentThread] threadDictionary] setObject: t
|
|
|
|
|
forKey: @"GSThroughput"];
|
|
|
|
|
RELEASE(t);
|
|
|
|
|
}
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-14 18:14:21 +00:00
|
|
|
|
+ (void) newSecond: (GSThroughputThread*)t
|
2005-10-29 16:07:38 +00:00
|
|
|
|
{
|
|
|
|
|
NSHashEnumerator e;
|
|
|
|
|
GSThroughput *i;
|
|
|
|
|
|
|
|
|
|
e = NSEnumerateHashTable(t->instances);
|
|
|
|
|
while ((i = (GSThroughput*)NSNextHashEnumeratorItem(&e)) != nil)
|
|
|
|
|
{
|
|
|
|
|
[i _update];
|
|
|
|
|
}
|
|
|
|
|
NSEndHashTableEnumeration(&e);
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
- (void) _detach
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->thread = nil;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
- (void) _update
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->thread != nil)
|
|
|
|
|
{
|
2005-11-14 18:14:21 +00:00
|
|
|
|
unsigned tick = GSTickerTimeTick();
|
2005-10-29 16:07:38 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
if (my->supportDurations == YES)
|
|
|
|
|
{
|
|
|
|
|
while (my->last < tick)
|
|
|
|
|
{
|
|
|
|
|
DInfo *info;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->second++ == 59)
|
|
|
|
|
{
|
|
|
|
|
info = &dminutes[my->minute];
|
|
|
|
|
for (i = 0; i < 60; i++)
|
|
|
|
|
{
|
|
|
|
|
DInfo *from = &dseconds[i];
|
|
|
|
|
|
|
|
|
|
info->cnt += from->cnt;
|
|
|
|
|
if (from->min < info->min)
|
|
|
|
|
{
|
|
|
|
|
info->min = from->min;
|
|
|
|
|
}
|
|
|
|
|
if (from->max > info->max)
|
|
|
|
|
{
|
|
|
|
|
info->max = from->max;
|
|
|
|
|
}
|
|
|
|
|
info->sum += from->sum;
|
|
|
|
|
}
|
2005-11-04 12:48:59 +00:00
|
|
|
|
if (my->minute++ == my->minutesPerPeriod - 1)
|
2005-10-29 16:07:38 +00:00
|
|
|
|
{
|
|
|
|
|
info = &dperiods[my->period];
|
|
|
|
|
for (i = 0; i < my->minutesPerPeriod; i++)
|
|
|
|
|
{
|
|
|
|
|
DInfo *from = &dminutes[i];
|
|
|
|
|
|
|
|
|
|
info->cnt += from->cnt;
|
|
|
|
|
if (from->min > 0.0 && from->min < info->min)
|
|
|
|
|
{
|
|
|
|
|
info->min = from->min;
|
|
|
|
|
}
|
|
|
|
|
if (from->max > info->max)
|
|
|
|
|
{
|
|
|
|
|
info->max = from->max;
|
|
|
|
|
}
|
|
|
|
|
info->sum += from->sum;
|
|
|
|
|
}
|
2005-11-04 12:48:59 +00:00
|
|
|
|
if (my->period++ == my->numberOfPeriods - 1)
|
2005-10-29 16:07:38 +00:00
|
|
|
|
{
|
|
|
|
|
my->period = 0;
|
|
|
|
|
}
|
|
|
|
|
info = &dperiods[my->period];
|
|
|
|
|
info->cnt = 0;
|
|
|
|
|
info->max = 0.0;
|
|
|
|
|
info->min = MAXDURATION;
|
|
|
|
|
info->sum = 0.0;
|
2005-11-04 12:08:50 +00:00
|
|
|
|
info->tick = my->last;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->minute = 0;
|
|
|
|
|
}
|
|
|
|
|
info = &dminutes[my->minute];
|
|
|
|
|
info->cnt = 0;
|
|
|
|
|
info->max = 0.0;
|
|
|
|
|
info->min = MAXDURATION;
|
|
|
|
|
info->sum = 0.0;
|
2005-11-04 12:08:50 +00:00
|
|
|
|
info->tick = my->last;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->second = 0;
|
|
|
|
|
}
|
|
|
|
|
info = &dseconds[my->second];
|
|
|
|
|
info->cnt = 0;
|
|
|
|
|
info->max = 0.0;
|
|
|
|
|
info->min = MAXDURATION;
|
|
|
|
|
info->sum = 0.0;
|
2005-11-04 12:08:50 +00:00
|
|
|
|
info->tick = my->last;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
|
|
|
|
|
my->last++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while (my->last < tick)
|
|
|
|
|
{
|
|
|
|
|
CInfo *info;
|
|
|
|
|
|
|
|
|
|
if (my->second++ == 59)
|
|
|
|
|
{
|
|
|
|
|
info = &cminutes[my->minute];
|
|
|
|
|
for (i = 0; i < 60; i++)
|
|
|
|
|
{
|
|
|
|
|
info->cnt += cseconds[i].cnt;
|
|
|
|
|
}
|
2005-11-04 12:48:59 +00:00
|
|
|
|
if (my->minute++ == my->minutesPerPeriod - 1)
|
2005-10-29 16:07:38 +00:00
|
|
|
|
{
|
|
|
|
|
info = &cperiods[my->period];
|
|
|
|
|
for (i = 0; i < my->minutesPerPeriod; i++)
|
|
|
|
|
{
|
|
|
|
|
info->cnt += cminutes[i].cnt;
|
|
|
|
|
}
|
2005-11-04 12:48:59 +00:00
|
|
|
|
if (my->period++ == my->numberOfPeriods - 1)
|
2005-10-29 16:07:38 +00:00
|
|
|
|
{
|
|
|
|
|
my->period = 0;
|
|
|
|
|
}
|
|
|
|
|
info = &cperiods[my->period];
|
|
|
|
|
info->cnt = 0;
|
2005-11-04 12:08:50 +00:00
|
|
|
|
info->tick = my->last;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->minute = 0;
|
|
|
|
|
}
|
|
|
|
|
info = &cminutes[my->minute];
|
|
|
|
|
info->cnt = 0;
|
2005-11-04 12:08:50 +00:00
|
|
|
|
info->tick = my->last;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->second = 0;
|
|
|
|
|
}
|
|
|
|
|
info = &cseconds[my->second];
|
|
|
|
|
info->cnt = 0;
|
2005-11-04 12:08:50 +00:00
|
|
|
|
info->tick = my->last;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
|
|
|
|
|
my->last++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSThroughput
|
|
|
|
|
|
|
|
|
|
+ (NSArray*) allInstances
|
|
|
|
|
{
|
|
|
|
|
GSThroughputThread *t;
|
|
|
|
|
NSArray *a;
|
|
|
|
|
|
|
|
|
|
t = [[[NSThread currentThread] threadDictionary]
|
|
|
|
|
objectForKey: @"GSThroughput"];
|
|
|
|
|
if (t == nil)
|
|
|
|
|
{
|
|
|
|
|
a = nil;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
a = NSAllHashTableObjects(t->instances);
|
|
|
|
|
}
|
|
|
|
|
return a;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (NSString*) description
|
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
GSThroughputThread *t;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
NSMutableString *ms;
|
|
|
|
|
|
|
|
|
|
ms = [NSMutableString stringWithString: [super description]];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
t = [[[NSThread currentThread] threadDictionary]
|
|
|
|
|
objectForKey: @"GSThroughput"];
|
|
|
|
|
if (t != nil)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
NSHashEnumerator e;
|
|
|
|
|
GSThroughput *c;
|
|
|
|
|
|
|
|
|
|
e = NSEnumerateHashTable(t->instances);
|
|
|
|
|
while ((c = (GSThroughput*)NSNextHashEnumeratorItem(&e)) != nil)
|
|
|
|
|
{
|
|
|
|
|
[ms appendFormat: @"\n%@", [c description]];
|
|
|
|
|
}
|
|
|
|
|
NSEndHashTableEnumeration(&e);
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (NSDateClass == 0)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
|
|
|
|
NSDateClass = [NSDate class];
|
|
|
|
|
tiSel = @selector(timeIntervalSinceReferenceDate);
|
|
|
|
|
tiImp
|
|
|
|
|
= (NSTimeInterval (*)(Class,SEL))[NSDateClass methodForSelector: tiSel];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-14 18:14:21 +00:00
|
|
|
|
+ (void) setTick: (BOOL)aFlag
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-11-14 18:14:21 +00:00
|
|
|
|
if (aFlag == YES)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-11-14 18:14:21 +00:00
|
|
|
|
GSThroughputThread *t = [self _threadInfo];
|
|
|
|
|
|
|
|
|
|
[GSTicker registerObserver: (id<GSTicker>)self userInfo: t];
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
2005-11-14 18:14:21 +00:00
|
|
|
|
else
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-11-14 18:14:21 +00:00
|
|
|
|
[GSTicker unregisterObserver: (id<GSTicker>)self];
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) tick
|
|
|
|
|
{
|
2005-11-14 18:14:21 +00:00
|
|
|
|
[self newSecond: [self _threadInfo]];
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) add: (unsigned)count
|
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
NSAssert(my->supportDurations == NO, @"configured for durations");
|
|
|
|
|
cseconds[my->second].cnt += count;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addDuration: (NSTimeInterval)length
|
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
DInfo *info;
|
|
|
|
|
|
|
|
|
|
NSAssert(my->supportDurations == YES, @"not configured for durations");
|
|
|
|
|
info = &dseconds[my->second];
|
|
|
|
|
if (info->cnt++ == 0)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
info->min = length;
|
|
|
|
|
info->max = length;
|
|
|
|
|
info->sum = length;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
info->sum += length;
|
|
|
|
|
if (length > info->max)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
info->max = length;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
if (length < info->min)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
info->min = length;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2005-11-04 06:00:41 +00:00
|
|
|
|
if (_data)
|
2005-10-29 16:07:38 +00:00
|
|
|
|
{
|
2005-11-04 06:00:41 +00:00
|
|
|
|
if (my->seconds != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), my->seconds);
|
|
|
|
|
}
|
|
|
|
|
RELEASE(my->name);
|
|
|
|
|
if (my->thread != nil)
|
|
|
|
|
{
|
|
|
|
|
NSHashRemove(my->thread->instances, (void*)self);
|
|
|
|
|
my->thread = nil;
|
|
|
|
|
}
|
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), _data);
|
|
|
|
|
_data = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-11-04 06:00:41 +00:00
|
|
|
|
[super dealloc];
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
CREATE_AUTORELEASE_POOL(pool);
|
2005-10-29 03:01:50 +00:00
|
|
|
|
NSString *n = my->name;
|
|
|
|
|
NSMutableString *m;
|
|
|
|
|
unsigned i;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
|
|
|
|
if (n == nil)
|
|
|
|
|
{
|
|
|
|
|
n = [super description];
|
|
|
|
|
}
|
2005-10-29 03:01:50 +00:00
|
|
|
|
m = [n mutableCopy];
|
2005-10-29 09:23:09 +00:00
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->thread != nil)
|
2005-10-29 03:01:50 +00:00
|
|
|
|
{
|
2005-11-14 18:14:21 +00:00
|
|
|
|
NSTimeInterval baseTime = GSTickerTimeStart();
|
2005-11-04 11:42:10 +00:00
|
|
|
|
unsigned tick;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
|
|
|
|
|
if (my->supportDurations == YES)
|
2005-10-29 03:01:50 +00:00
|
|
|
|
{
|
2005-11-04 11:56:52 +00:00
|
|
|
|
[m appendString: @"\nSeconds in current minute:\n"];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->second > 0)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
2005-11-04 12:08:50 +00:00
|
|
|
|
tick = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
for (i = 0; i < my->second; i++)
|
|
|
|
|
{
|
|
|
|
|
DInfo *info = &dseconds[i];
|
|
|
|
|
NSTimeInterval ti = info->tick + baseTime;
|
2005-10-29 03:01:50 +00:00
|
|
|
|
|
2005-11-04 11:42:10 +00:00
|
|
|
|
if (info->tick != tick)
|
|
|
|
|
{
|
|
|
|
|
tick = info->tick;
|
|
|
|
|
[m appendFormat: @"%u, %g, %g, %g, %@\n",
|
|
|
|
|
info->cnt, info->max, info->min, info->sum,
|
|
|
|
|
[NSDate dateWithTimeIntervalSinceReferenceDate: ti]];
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
2005-10-29 03:01:50 +00:00
|
|
|
|
|
2005-11-04 11:56:52 +00:00
|
|
|
|
[m appendString: @"\nPrevious minutes in current period:\n"];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->minute > 0)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
2005-11-04 12:08:50 +00:00
|
|
|
|
tick = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
for (i = 0; i < my->minute; i++)
|
|
|
|
|
{
|
|
|
|
|
DInfo *info = &dminutes[i];
|
|
|
|
|
NSTimeInterval ti = info->tick + baseTime;
|
2005-10-29 03:01:50 +00:00
|
|
|
|
|
2005-11-04 11:42:10 +00:00
|
|
|
|
if (info->tick != tick)
|
|
|
|
|
{
|
|
|
|
|
tick = info->tick;
|
|
|
|
|
[m appendFormat: @"%u, %g, %g, %g, %@\n",
|
|
|
|
|
info->cnt, info->max, info->min, info->sum,
|
|
|
|
|
[NSDate dateWithTimeIntervalSinceReferenceDate: ti]];
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
2005-10-29 03:01:50 +00:00
|
|
|
|
|
2005-11-04 11:56:52 +00:00
|
|
|
|
[m appendString: @"\nPrevious periods:\n"];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->period > 0)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
2005-11-04 12:08:50 +00:00
|
|
|
|
tick = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
for (i = 0; i < my->period; i++)
|
|
|
|
|
{
|
|
|
|
|
DInfo *info = &dperiods[i];
|
|
|
|
|
NSTimeInterval ti = info->tick + baseTime;
|
2005-10-29 09:23:09 +00:00
|
|
|
|
|
2005-11-04 11:42:10 +00:00
|
|
|
|
if (info->tick != tick)
|
|
|
|
|
{
|
|
|
|
|
tick = info->tick;
|
|
|
|
|
[m appendFormat: @"%u, %g, %g, %g, %@\n",
|
|
|
|
|
info->cnt, info->max, info->min, info->sum,
|
|
|
|
|
[NSDate dateWithTimeIntervalSinceReferenceDate: ti]];
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
else
|
2005-10-29 03:01:50 +00:00
|
|
|
|
{
|
2005-11-04 11:56:52 +00:00
|
|
|
|
[m appendString: @"\nSeconds in current minute:\n"];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->second > 0)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
2005-11-04 12:08:50 +00:00
|
|
|
|
tick = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
for (i = 0; i < my->second; i++)
|
|
|
|
|
{
|
|
|
|
|
CInfo *info = &cseconds[i];
|
|
|
|
|
NSTimeInterval ti = info->tick + baseTime;
|
2005-10-29 03:01:50 +00:00
|
|
|
|
|
2005-11-04 11:42:10 +00:00
|
|
|
|
if (info->tick != tick)
|
|
|
|
|
{
|
|
|
|
|
tick = info->tick;
|
|
|
|
|
[m appendFormat: @"%u, %@\n", info->cnt,
|
|
|
|
|
[NSDate dateWithTimeIntervalSinceReferenceDate: ti]];
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-04 11:56:52 +00:00
|
|
|
|
[m appendString: @"\nPrevious minutes in current period:\n"];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->minute > 0)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
2005-11-04 12:08:50 +00:00
|
|
|
|
tick = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
for (i = 0; i < my->minute; i++)
|
|
|
|
|
{
|
|
|
|
|
CInfo *info = &cminutes[i];
|
|
|
|
|
NSTimeInterval ti = info->tick + baseTime;
|
2005-10-29 09:23:09 +00:00
|
|
|
|
|
2005-11-04 11:42:10 +00:00
|
|
|
|
if (info->tick != tick)
|
|
|
|
|
{
|
|
|
|
|
tick = info->tick;
|
|
|
|
|
[m appendFormat: @"%u, %@\n", info->cnt,
|
|
|
|
|
[NSDate dateWithTimeIntervalSinceReferenceDate: ti]];
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-04 11:56:52 +00:00
|
|
|
|
[m appendString: @"\nPrevious periods:\n"];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->period > 0)
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
2005-11-04 12:08:50 +00:00
|
|
|
|
tick = 0;
|
2005-10-29 16:07:38 +00:00
|
|
|
|
for (i = 0; i < my->period; i++)
|
|
|
|
|
{
|
|
|
|
|
CInfo *info = &cperiods[i];
|
|
|
|
|
NSTimeInterval ti = info->tick + baseTime;
|
2005-10-29 09:23:09 +00:00
|
|
|
|
|
2005-11-04 11:42:10 +00:00
|
|
|
|
if (info->tick != tick)
|
|
|
|
|
{
|
|
|
|
|
tick = info->tick;
|
|
|
|
|
[m appendFormat: @"%u, %@\n", info->cnt,
|
|
|
|
|
[NSDate dateWithTimeIntervalSinceReferenceDate: ti]];
|
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
}
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
2005-10-29 03:01:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-29 09:23:09 +00:00
|
|
|
|
DESTROY(pool);
|
2005-10-29 03:01:50 +00:00
|
|
|
|
return AUTORELEASE(m);
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-10-29 09:23:09 +00:00
|
|
|
|
- (void) endDuration
|
|
|
|
|
{
|
|
|
|
|
NSAssert(my->started > 0.0, NSInternalInconsistencyException);
|
|
|
|
|
[self addDuration: (*tiImp)(NSDateClass, tiSel) - my->started];
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->event = nil;
|
2005-10-29 09:23:09 +00:00
|
|
|
|
my->started = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-28 16:38:24 +00:00
|
|
|
|
- (id) init
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
|
|
|
|
return [self initWithDurations: YES
|
|
|
|
|
forPeriods: 96
|
|
|
|
|
ofLength: 15];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithDurations: (BOOL)aFlag
|
|
|
|
|
forPeriods: (unsigned)numberOfPeriods
|
|
|
|
|
ofLength: (unsigned)minutesPerPeriod
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 03:01:50 +00:00
|
|
|
|
NSCalendarDate *c;
|
|
|
|
|
unsigned i;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
|
2005-11-04 06:00:41 +00:00
|
|
|
|
_data = (Item*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(Item));
|
|
|
|
|
memset(_data, '\0', sizeof(Item));
|
|
|
|
|
|
2005-10-29 09:23:09 +00:00
|
|
|
|
if (numberOfPeriods < 1 || minutesPerPeriod < 1)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 09:23:09 +00:00
|
|
|
|
DESTROY(self);
|
|
|
|
|
return nil;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Add this instance to the current thread.
|
|
|
|
|
*/
|
|
|
|
|
my->thread = [[self class] _threadInfo];
|
|
|
|
|
NSHashInsert(my->thread->instances, (void*)self);
|
|
|
|
|
|
2005-10-29 09:23:09 +00:00
|
|
|
|
my->supportDurations = aFlag;
|
|
|
|
|
my->numberOfPeriods = numberOfPeriods;
|
|
|
|
|
my->minutesPerPeriod = minutesPerPeriod;
|
2005-11-14 18:14:21 +00:00
|
|
|
|
my->last = GSTickerTimeTick();
|
2005-10-29 16:07:38 +00:00
|
|
|
|
c = [[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:
|
2005-11-14 18:14:21 +00:00
|
|
|
|
GSTickerTimeLast()];
|
2005-10-29 03:01:50 +00:00
|
|
|
|
my->second = [c secondOfMinute];
|
2005-10-29 09:23:09 +00:00
|
|
|
|
i = [c hourOfDay] * 60 + [c minuteOfHour];
|
|
|
|
|
my->minute = i % minutesPerPeriod;
|
2005-11-04 12:48:59 +00:00
|
|
|
|
my->period = (i / minutesPerPeriod) % numberOfPeriods;
|
2005-10-29 03:01:50 +00:00
|
|
|
|
RELEASE(c);
|
2005-10-29 09:23:09 +00:00
|
|
|
|
|
|
|
|
|
i = 60 + minutesPerPeriod + numberOfPeriods;
|
|
|
|
|
if (my->supportDurations == YES)
|
|
|
|
|
{
|
|
|
|
|
DInfo *ptr;
|
|
|
|
|
|
|
|
|
|
ptr = (DInfo*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(DInfo) * i);
|
|
|
|
|
memset(ptr, '\0', sizeof(DInfo) * i);
|
|
|
|
|
my->seconds = ptr;
|
2005-11-04 12:48:59 +00:00
|
|
|
|
my->minutes = ptr + 60;
|
|
|
|
|
my->periods = ptr + 60 + minutesPerPeriod;
|
2005-10-29 09:23:09 +00:00
|
|
|
|
dseconds[my->second].tick = my->last;
|
|
|
|
|
dminutes[my->minute].tick = my->last;
|
|
|
|
|
dperiods[my->period].tick = my->last;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < my->numberOfPeriods; i++)
|
|
|
|
|
{
|
|
|
|
|
dperiods[i].min = MAXDURATION;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < my->minutesPerPeriod; i++)
|
|
|
|
|
{
|
|
|
|
|
dminutes[i].min = MAXDURATION;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < 60; i++)
|
|
|
|
|
{
|
|
|
|
|
dseconds[i].min = MAXDURATION;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CInfo *ptr;
|
|
|
|
|
|
|
|
|
|
ptr = (CInfo*)NSZoneMalloc(NSDefaultMallocZone(), sizeof(CInfo) * i);
|
|
|
|
|
memset(ptr, '\0', sizeof(CInfo) * i);
|
|
|
|
|
my->seconds = ptr;
|
2005-11-04 12:48:59 +00:00
|
|
|
|
my->minutes = ptr + 60;
|
|
|
|
|
my->periods = ptr + 60 + minutesPerPeriod;
|
2005-11-04 06:00:41 +00:00
|
|
|
|
cseconds[my->second].tick = my->last;
|
|
|
|
|
cminutes[my->minute].tick = my->last;
|
|
|
|
|
cperiods[my->period].tick = my->last;
|
2005-10-29 09:23:09 +00:00
|
|
|
|
}
|
2005-10-28 16:38:24 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) name
|
|
|
|
|
{
|
|
|
|
|
return my->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setName: (NSString*)name
|
|
|
|
|
{
|
|
|
|
|
ASSIGN(my->name, name);
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-29 16:07:38 +00:00
|
|
|
|
- (void) startDuration: (NSString*)name
|
2005-10-29 09:23:09 +00:00
|
|
|
|
{
|
|
|
|
|
NSAssert(my->supportDurations == YES && my->started == 0.0,
|
|
|
|
|
NSInternalInconsistencyException);
|
2005-10-29 16:07:38 +00:00
|
|
|
|
if (my->event != nil)
|
2005-10-28 16:38:24 +00:00
|
|
|
|
{
|
2005-10-29 16:07:38 +00:00
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"-startDuration: for '%@' nested inside '%@'",
|
|
|
|
|
my->event, name];
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
my->started = (*tiImp)(NSDateClass, tiSel);
|
|
|
|
|
my->event = name;
|
2005-10-28 16:38:24 +00:00
|
|
|
|
}
|
2005-10-29 16:07:38 +00:00
|
|
|
|
|
2005-10-28 16:38:24 +00:00
|
|
|
|
@end
|
|
|
|
|
|