1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @brief Dynamic Knobs Initialization for Core.
27 ******************************************************************************/
30 #include <core/knobs.h>
36 // Assume the type is compatible with a 32-bit integer
38 static inline void ConvertEnvToKnob(const char* pOverride
, T
& knobValue
)
41 if (sscanf(pOverride
, "%u", &value
))
43 knobValue
= static_cast<T
>(value
);
47 static inline void ConvertEnvToKnob(const char* pOverride
, bool& knobValue
)
49 size_t len
= strlen(pOverride
);
52 auto c
= tolower(pOverride
[0]);
53 if (c
== 'y' || c
== 't' || c
== '1')
58 if (c
== 'n' || c
== 'f' || c
== '0')
65 // Try converting to a number and casting to bool
67 if (sscanf(pOverride
, "%u", &value
))
69 knobValue
= value
!= 0;
74 static inline void ConvertEnvToKnob(const char* pOverride
, float& knobValue
)
76 float value
= knobValue
;
77 if (sscanf(pOverride
, "%f", &value
))
83 static inline void ConvertEnvToKnob(const char* pOverride
, std::string
& knobValue
)
85 knobValue
= pOverride
;
89 static inline void InitKnob(T
& knob
)
92 // TODO, read registry first
94 // Second, read environment variables
95 const char* pOverride
= getenv(knob
.Name());
99 auto knobValue
= knob
.Value();
100 ConvertEnvToKnob(pOverride
, knobValue
);
101 knob
.Value(knobValue
);