threads / patch / 37639

patchDo not make trace.c/getnanotime an inlined function

Subject: [PATCH] Do not make trace.c/getnanotime an inlined function

## tl;dr

7 messages between Sep 28, 2014 and Sep 30, 2014. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Ben Walton· Sep 28, 2014, 07:50 UTC · lore

Oracle Studio compilers don't allow for static variables in functions that are defined to be inline. GNU C does permit this. Let's reference the C99 standard though, which doesn't allow for inline functions to contain modifiable static variables.

Signed-off-by: Ben Walton <bdwalton@gmail.com>
---
 trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to trace.c +1 −1
diff --git a/trace.c b/trace.c
index b6f25a2..4778608 100644
--- a/trace.c
+++ b/trace.c
@@ -385,7 +385,7 @@ static inline uint64_t gettimeofday_nanos(void)
  * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
  * (i.e. favoring high precision over wall clock time accuracy).
  */
-inline uint64_t getnanotime(void)
+uint64_t getnanotime(void)
 {
 	static uint64_t offset;
 	if (offset > 1) {
-- 
1.9.1
Johannes Sixt· Sep 28, 2014, 19:15 UTC · re: Ben Walton · lore

Re: [PATCH] Do not make trace.c/getnanotime an inlined function

Am 28.09.2014 um 09:50 schrieb Ben Walton:
Show 24 quoted lines
> Oracle Studio compilers don't allow for static variables in functions
> that are defined to be inline. GNU C does permit this. Let's reference
> the C99 standard though, which doesn't allow for inline functions to
> contain modifiable static variables.
> 
> Signed-off-by: Ben Walton <bdwalton@gmail.com>
> ---
>  trace.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/trace.c b/trace.c
> index b6f25a2..4778608 100644
> --- a/trace.c
> +++ b/trace.c
> @@ -385,7 +385,7 @@ static inline uint64_t gettimeofday_nanos(void)
>   * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
>   * (i.e. favoring high precision over wall clock time accuracy).
>   */
> -inline uint64_t getnanotime(void)
> +uint64_t getnanotime(void)
>  {
>  	static uint64_t offset;
>  	if (offset > 1) {
> 
But then the function could stay static, no?
-- Hannes
Duy Nguyen· Sep 29, 2014, 12:44 UTC · re: Ben Walton · lore

Re: [PATCH] Do not make trace.c/getnanotime an inlined function

On Sun, Sep 28, 2014 at 2:50 PM, Ben Walton <bdwalton@gmail.com> wrote:
Show 22 quoted lines
> Oracle Studio compilers don't allow for static variables in functions
> that are defined to be inline. GNU C does permit this. Let's reference
> the C99 standard though, which doesn't allow for inline functions to
> contain modifiable static variables.
>
> Signed-off-by: Ben Walton <bdwalton@gmail.com>
> ---
>  trace.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/trace.c b/trace.c
> index b6f25a2..4778608 100644
> --- a/trace.c
> +++ b/trace.c
> @@ -385,7 +385,7 @@ static inline uint64_t gettimeofday_nanos(void)
>   * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
>   * (i.e. favoring high precision over wall clock time accuracy).
>   */
> -inline uint64_t getnanotime(void)
> +uint64_t getnanotime(void)
>  {
>         static uint64_t offset;
Would moving this offset outside getnanotime() work?
-- 
Duy
Junio C Hamano· Sep 29, 2014, 17:48 UTC · re: Duy Nguyen · lore

Re: [PATCH] Do not make trace.c/getnanotime an inlined function

Duy Nguyen <pclouds@gmail.com> writes:
Show 25 quoted lines
> On Sun, Sep 28, 2014 at 2:50 PM, Ben Walton <bdwalton@gmail.com> wrote:
>> Oracle Studio compilers don't allow for static variables in functions
>> that are defined to be inline. GNU C does permit this. Let's reference
>> the C99 standard though, which doesn't allow for inline functions to
>> contain modifiable static variables.
>>
>> Signed-off-by: Ben Walton <bdwalton@gmail.com>
>> ---
>>  trace.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/trace.c b/trace.c
>> index b6f25a2..4778608 100644
>> --- a/trace.c
>> +++ b/trace.c
>> @@ -385,7 +385,7 @@ static inline uint64_t gettimeofday_nanos(void)
>>   * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
>>   * (i.e. favoring high precision over wall clock time accuracy).
>>   */
>> -inline uint64_t getnanotime(void)
>> +uint64_t getnanotime(void)
>>  {
>>         static uint64_t offset;
>
> Would moving this offset outside getnanotime() work?
I am not sure what the definition of "work" is.

The function computes the difference between the returned value from gettimeofday(2) and a custom highres_nanos() just once and returns the value it got from gettimeofday the first time, and then for subsequent calls massages the returned value from highres_nanos() to be consistent with the value returned from gettimeofday using the offset it computed in the first call.

If we have two copies of this function, two independent probes to these pair of underlying functions will be made to compute their offsets. With perfect pair of clocks that may not matter, but it just feels wrong to me.

Besides, I wonder what happens if the computed offset happen to be 1, which is used as a sentinel.

Duy Nguyen· Sep 30, 2014, 09:25 UTC · re: Junio C Hamano · lore

Re: [PATCH] Do not make trace.c/getnanotime an inlined function

On Tue, Sep 30, 2014 at 12:48 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 40 quoted lines
> Duy Nguyen <pclouds@gmail.com> writes:
>
>> On Sun, Sep 28, 2014 at 2:50 PM, Ben Walton <bdwalton@gmail.com> wrote:
>>> Oracle Studio compilers don't allow for static variables in functions
>>> that are defined to be inline. GNU C does permit this. Let's reference
>>> the C99 standard though, which doesn't allow for inline functions to
>>> contain modifiable static variables.
>>>
>>> Signed-off-by: Ben Walton <bdwalton@gmail.com>
>>> ---
>>>  trace.c | 2 +-
>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/trace.c b/trace.c
>>> index b6f25a2..4778608 100644
>>> --- a/trace.c
>>> +++ b/trace.c
>>> @@ -385,7 +385,7 @@ static inline uint64_t gettimeofday_nanos(void)
>>>   * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
>>>   * (i.e. favoring high precision over wall clock time accuracy).
>>>   */
>>> -inline uint64_t getnanotime(void)
>>> +uint64_t getnanotime(void)
>>>  {
>>>         static uint64_t offset;
>>
>> Would moving this offset outside getnanotime() work?
>
> I am not sure what the definition of "work" is.
>
> The function computes the difference between the returned value from
> gettimeofday(2) and a custom highres_nanos() just once and returns
> the value it got from gettimeofday the first time, and then for
> subsequent calls massages the returned value from highres_nanos() to
> be consistent with the value returned from gettimeofday using the
> offset it computed in the first call.
>
> If we have two copies of this function, two independent probes to
> these pair of underlying functions will be made to compute their
> offsets.

Hmm.. no. Even if the function is inlined in multiple places, inline code still points to the same "offset" variable. So the gettimeofday_nanos()/highres_nanos() pair should only be called once. Tested with gcc.

-- 
Duy
Junio C Hamano· Sep 30, 2014, 15:54 UTC · re: Duy Nguyen · lore

Re: [PATCH] Do not make trace.c/getnanotime an inlined function

Duy Nguyen <pclouds@gmail.com> writes:
> Hmm.. no. Even if the function is inlined in multiple places, inline
> code still points to the same "offset" variable.
OK, thanks.
Johannes Sixt· Sep 29, 2014, 20:40 UTC · lore

Re: [PATCH] Do not make trace.c/getnanotime an inlined function

Am 28.09.2014 um 22:42 schrieb Ben Walton:
Show 11 quoted lines
> On Sun, Sep 28, 2014 at 8:15 PM, Johannes Sixt <j6t@kdbg.org
> <mailto:j6t@kdbg.org>> wrote:
>     Am 28.09.2014 um 09:50 schrieb Ben Walton:
>     > -inline uint64_t getnanotime(void)
>     > +uint64_t getnanotime(void)
> 
>     But then the function could stay static, no?
> 
> 
> This function is used in several places outside of the translation unit
> so while it's possible, I think it's more work than it's worth...

I see. I didn't check myself, sorry. I assumed that due to the 'inline' the function would not have been available outside. Now your patch looks good.

Thanks, -- Hannes

← back to recent threads