1 //===========================================================================
5 // ISO C date and time implementation for clock()
7 //===========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //===========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): jlarmour
44 // Contributors: jlarmour
46 // Purpose: Provides an implementation of the ISO C function clock()
47 // from ISO C section 7.12.2.1
48 // Description: This file uses the kernel real time clock to determine
49 // the complete running time of the system - since we only
50 // have one task, even though there are perhaps multiple threads
51 // that is still the running time of the "program"
54 //####DESCRIPTIONEND####
56 //===========================================================================
60 #include <pkgconf/libc_time.h> // Configuration header
62 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
63 # include <pkgconf/kernel.h> // Kernel config header
68 #include <cyg/infra/cyg_type.h> // Common type definitions and support
69 #include <cyg/infra/cyg_ass.h> // Assertion infrastructure
70 #include <cyg/infra/cyg_trac.h> // Tracing infrastructure
72 #include <time.h> // Header for all time-related functions
74 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
75 # include <cyg/kernel/clock.hxx> // Kernel clock definitions
76 # include <cyg/kernel/clock.inl> // Kernel clock inline functions
82 # if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_TIME_CLOCK_TRACE_LEVEL)
83 static int clock_trace = CYGNUM_LIBC_TIME_CLOCK_TRACE_LEVEL;
84 # define TL1 (0 < clock_trace)
94 CYG_REPORT_FUNCNAMETYPE( "clock", "returning clock tick %d" );
95 CYG_REPORT_FUNCARGVOID();
97 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
98 cyg_tick_count curr_clock; // kernel clock value
99 Cyg_Clock::cyg_resolution resolution; // kernel clock resolution
101 unsigned long long temp;
103 CYG_TRACE0( TL1, "getting clock resolution" );
105 // get the resolution
106 resolution = Cyg_Clock::real_time_clock->get_resolution();
108 CYG_TRACE2( TL1, "got resolution dividend %d divisor %d. Getting "
109 "clock value", resolution.dividend, resolution.divisor );
112 curr_clock = Cyg_Clock::real_time_clock->current_value();
114 CYG_TRACE1( TL1, "got clock value %d", curr_clock );
116 // scale the value so that clock()/CLOCKS_PER_SEC works
117 // We use an unsigned long long to avoid overflow as the dividend
118 // and divisors tend to be huge
119 temp = (1000000000 / CLOCKS_PER_SEC);
120 temp *= resolution.divisor;
121 temp = (unsigned long long)curr_clock * resolution.dividend / temp;
122 clocks = (clock_t)temp;
124 CYG_REPORT_RETVAL( clocks );
127 #else // i.e. ifndef CYGSEM_LIBC_TIME_CLOCK_WORKING
128 CYG_REPORT_RETVAL( -1 );