summaryrefslogtreecommitdiff
path: root/tests/test-util.c
Commit message (Collapse)AuthorAgeFilesLines
* Add read-only option to ovs-dpctl and ovs-ofctl commands.Ryan Moats2016-08-151-17/+17
| | | | | | | | | ovs-dpctl and ovs-ofctl lack a read-only option to prevent running of commands that perform read-write operations. Add it and the necessary scaffolding to each. Signed-off-by: Ryan Moats <rmoats@us.ibm.com> Signed-off-by: Ben Pfaff <blp@ovn.org>
* lib/util.c: Optimise bitwise_rscan.Han Zhou2016-03-231-1/+102
| | | | | | | | | | | | | | | | | | | | | | | bitwise_rscan() is found to be hot spot in ovn-controller during OVN scalability tests. It is triggered by lflow_run() when processing lflow updates from SB ovsdb. The perf result shows: + 35.90% ovn-controller ovn-controller [.] bitwise_rscan + 13.39% ovn-controller [kernel.kallsyms] [k] 0xffffffff8104f45a + 5.02% ovn-controller libc-2.19.so [.] _int_malloc + 3.47% ovn-controller libc-2.19.so [.] _int_free After optimization, bitwise_rscan percentage dropped from 36% to less than 6%: + 11.34% ovn-controller [kernel.kallsyms] [k] 0xffffffff8104f45a + 8.15% ovn-controller libc-2.19.so [.] _int_malloc + 5.77% ovn-controller ovn-controller [.] bitwise_rscan + 5.49% ovn-controller libc-2.19.so [.] _int_free Signed-off-by: Han Zhou <zhouhan@gmail.com> [blp@ovn.org enhanced the test] Signed-off-by: Ben Pfaff <blp@ovn.org>
* configure: Stop avoiding -Wformat-zero-length.Ben Pfaff2015-06-101-2/+1
| | | | | | | | | | | | | | | | | | | Debian likes to enable -Wformat-zero-length, even over our code trying to disable it. It isn't too hard to make our code warning-free against this option, so this commit both stops disabling it and fixes the warnings. The first fix is to change set_subprogram_name() to take a plain string instead of a format string, and to adjust its few callers. This fixes one warning since one of those callers passed in an empty string. The second fix is to remove a test for ovs_scan() against an empty string. I couldn't find a way to avoid a warning for this test, and it isn't too valuable in any case. This allows us to drop filtering for -Wformat from the Debian rules file, so this commit removes it. Signed-off-by: Ben Pfaff <blp@nicira.com>
* command-line: add ovs_cmdl_contextRussell Bryant2015-03-171-21/+24
| | | | | | | | | | | | | | | | | | | | | | | I started working on a new command line utility that used this shared code. I wanted the ability to pass some data from common initialization code to all of the commands. You can find a similar pattern in ovs-vsctl. This patch updates the command handler to take a new struct, ovs_cmdl_context, instead of argc and argv directly. It includes argc and argv, but also includes an opaque type (void *), where the user of this API can attach its custom data it wants passed along to command handlers. This patch affected the ovstest sub-programs, as well. The patch includes a bit of an odd hack to OVSTEST_REGISTER() to avoid making the main() function of the sub-programs take a ovs_cmdl_context. The test main() functions still receive argc and argv directly, as that seems more natural. The test-subprograms themselves are able to make use of a context internally, though. Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* command-line: add ovs_cmdl_ prefixRussell Bryant2015-03-161-3/+3
| | | | | | | | | | | | | | | | | The coding style guidelines include the following: - Pick a unique name prefix (ending with an underscore) for each module, and apply that prefix to all of that module's externally visible names. Names of macro parameters, struct and union members, and parameters in function prototypes are not considered externally visible for this purpose. This patch adds the new prefix to the externally visible names. This makes it a bit more obvious what code is coming from common command line handling code. Signed-off-by: Russell Bryant <rbryant@redhat.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* lib: Move vlog.h to <openvswitch/vlog.h>Thomas Graf2014-12-151-1/+1
| | | | | | | | A new function vlog_insert_module() is introduced to avoid using list_insert() from the vlog.h header. Signed-off-by: Thomas Graf <tgraf@noironetworks.com> Acked-by: Ben Pfaff <blp@nicira.com>
* ovs_assert, tests: Support NDEBUG.Jarno Rajahalme2014-10-301-7/+4
| | | | | | | | | | | | | | | | | ./configure accepts --enable-ndebug option. Make ovs_assert() honor it, and make sure all test programs disable it. The order of include files in test programs is also made uniform: 1. #include <config.h> 2. #undef NDEBUG 3. Include file of the test subject (to make sure it itself has sufficient include directives). 4. System includes in alphapetical order. 5. OVS includes in aplhapetical order. Suggested-by: Ben Pfaff <blp@nicira.com> Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* command-line: Add function to print command usage.Alex Wang2014-10-281-16/+16
| | | | | | | | | | | | | This commit adds a new variable in 'struct command' for recording the command usage. Also, a new function is added to print the usage given the array of defined commands. Later patch will use the output in bash command-line completion script. Signed-off-by: Alex Wang <alexw@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Don't compile couple of unused function for Windows.Gurucharan Shetty2014-06-241-0/+23
| | | | | | | | | | | | basename() and dir_name() are not used for Windows and won't work well if used. So put a '#ifndef _WIN32' around them to prevent future calls. test-file_name.c tests the above 2 functions. It makes sense to merge this single function file with test-util.c and then not compile it for Windows. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* test-util: Changes for 'assert' test on Windows.Gurucharan Shetty2014-06-241-0/+5
| | | | | | | | | | | | | There is no 'kill -l' type functionality available on Windows. So instead of looking for the string 'ABRT', check for the exit code in both platforms. On msys (unit test environment), it is 9 and on Linux, it is 134 (SIGABRT + 128). On Windows, stderr is fully buffered if connected to a pipe. Make it _IONBF so that an abort does not miss log contents. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* unit-test: Link 29 test programs into ovstestAndy Zhou2014-04-031-4/+6
| | | | | | | | | | | | | | Improve link speed by linking 29 test programs into ovstest. On my machine, running the following command against a fully built tree: $ touch lib/random.c; time make Improve the overall build time from 7 seconds to 3.5 seconds. Signed-off-by: Andy Zhou <azhou@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* stdio: New module, initially to provide working [v]snprintf() on Windows.Ben Pfaff2013-12-241-0/+18
| | | | | | | | This should transparently define snprintf() and vsnprintf() wrappers for use on Windows. CC: Saurabh Shah <ssaurabh@vmware.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* lib/util: Add clz32() and clz64().Jarno Rajahalme2013-12-031-0/+53
| | | | | | | | | Count leading zeroes using builtin if available. Make log_2_floor() use raw_clz() and inline log_2_floor() and log_2_ceil(). Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* lib/util: Rename ctz() as ctz32().Jarno Rajahalme2013-12-031-8/+8
| | | | | | | ctz() returns 32 for zero input, and we already have ctz64(), so it makes sense to rename ctz() as ctz32(). Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Rename popcount to count_1bitsBen Pfaff2013-11-191-9/+9
| | | | | | | | | | | This avoids a conflict with NetBSD's strings.h/libc. (http://netbsd.gw.com/cgi-bin/man-cgi?popcount++NetBSD-current) The new name is suggested by Ben Pfaff. Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@gmail.com>
* util: Make popcount() handle 64-bit integers, not separate popcount64().Ben Pfaff2013-11-181-34/+4
| | | | | | | | | | | | | | | Having a single function that can do popcount() on any integer type is easier for callers to get right. The implementation is probably slower if the caller actually provides a 32-bit (or shorter) integer, but the only existing callers always provide a full 64-bit integer so this seems unimportant for now. This also restores use, in practice, of the optimized implementation of population count. (As the comment on popcount32() says, this version is 2x faster than __builtin_popcount().) Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
* lib/util: Add ctz64() and popcount64().Jarno Rajahalme2013-11-181-7/+60
| | | | | | | Add raw_ctz64(), ctz64(), and popcount64() using builtins when available. Signed-off By: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: New function ovs_scan().Ben Pfaff2013-11-151-0/+533
| | | | | | | | | | This new function is essentially an implementation of sscanf() with slightly different behavior (see the comment) that is more convenient for Open vSwitch internal use. Also, this implementation ought to work out of the box on Windows, which has a defective sscanf() that lacks the 'hh' modifier required to scan into a char variable. Signed-off-by: Ben Pfaff <blp@nicira.com>
* openvswitch/types.h: New macros OVS_BE16_MAX, OVS_BE32_MAX, OVS_BE64_MAX.Ben Pfaff2013-09-171-1/+1
| | | | | | These seem slightly nicer than e.g. htons(UINT16_MAX). Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: New macros ROUND_UP_POW2, ROUND_DOWN_POW2.Ben Pfaff2013-07-171-0/+65
| | | | Signed-off-by: Ben Pfaff <blp@nicira.com>
* Use random_*() instead of rand(), for thread safety.Ben Pfaff2013-06-281-3/+3
| | | | | | | | None of these test programs are threaded, but has little cost and means that "grep" doesn't turn up any instances of these thread-unsafe functions in our tree. Signed-off-by: Ben Pfaff <blp@nicira.com>
* Make most "struct option" instances "const".Ben Pfaff2013-05-031-1/+1
| | | | | | | | Reducing non-const static data makes code more obviously thread-safe. Although option parsing does not normally need to be thread-safe, I don't know of a drawback to making its data const. Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: Introduce ovs_assert macro.Ben Pfaff2013-01-161-1/+42
| | | | | | | | | | | An occasionally significant problem with the standard "assert" macro is that it writes the failure message to stderr. In our daemons, stderr is generally redirected to /dev/null. It's more useful to write the failure message to the log, which is what the new ovs_assert macro introduced in this patch does. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Ethan Jackson <ethan@nicira.com>
* util: New function popcount().Ben Pfaff2012-09-041-0/+57
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the fastest portable implementation among the ones below, as measured with GCC 4.4 on a Xeon X3430. The measeured times were, in seconds: popcount1 25.6 popcount2 6.9 (but is not portable) popcount3 31.4 popcount4 25.6 popcount5 61.6 (and is buggy) popcount6 64.6 popcount7 32.3 popcount8 11.2 int popcount1(unsigned int x) { return __builtin_popcount(x); } int popcount2(unsigned int x) { unsigned int y; asm("popcnt %1, %0" : "=r" (y) : "g" (x)); return y; } int popcount3(unsigned int x) { unsigned int n; n = (x >> 1) & 033333333333; x -= n; n = (n >> 1) & 033333333333; x -= n; x = (x + (x >> 3)) & 030707070707; return x % 63; } int popcount4(unsigned int x) { x -= (x >> 1) & 0x55555555; x = (x & 0x33333333) + ((x >> 2) & 0x33333333); x = (x + (x >> 4)) & 0x0f0f0f0f; x += x >> 8; x += x >> 16; return x & 0x3f; } int popcount5(unsigned int x) { int n; n = 0; while (x) { if (x & 0xf) { n += ((0xe9949440 >> (x & 0xf)) & 3) + 1; } x >>= 4; } return n; } int popcount6(unsigned int x) { int n; n = 0; while (x) { n += (0xe994 >> (x & 7)) & 3; x >>= 3; } return n; } int popcount7(unsigned int x) { static const int table[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; return (table[x & 0xf] + table[(x >> 4) & 0xf] + table[(x >> 8) & 0xf] + table[(x >> 12) & 0xf] + table[(x >> 16) & 0xf] + table[(x >> 20) & 0xf] + table[(x >> 24) & 0xf] + table[x >> 28]); } static int popcount8(unsigned int x) { ((((X) & (1 << 0)) != 0) + \ (((X) & (1 << 1)) != 0) + \ (((X) & (1 << 2)) != 0) + \ (((X) & (1 << 3)) != 0) + \ (((X) & (1 << 4)) != 0) + \ (((X) & (1 << 5)) != 0) + \ (((X) & (1 << 6)) != 0) + \ (((X) & (1 << 7)) != 0)) static const uint8_t popcount8[256] = { INIT64(0), INIT64(64), INIT64(128), INIT64(192) }; return (popcount8[x & 0xff] + popcount8[(x >> 8) & 0xff] + popcount8[(x >> 16) & 0xff] + popcount8[x >> 24]); } int main(void) { unsigned long long int x; int n; n = 0; for (x = 0; x <= UINT32_MAX; x++) { n += popcount8(x); } printf("%d\n", n); return 0; } Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: New function follow_symlinks().Ben Pfaff2012-08-011-0/+13
| | | | | | It will acquire its first user in an upcoming commit. Signed-off-by: Ben Pfaff <blp@nicira.com>
* tests: Slightly generalize utility function tests.Ben Pfaff2012-08-011-33/+57
| | | | | | This will allow passing arguments in for an upcoming test. Signed-off-by: Ben Pfaff <blp@nicira.com>
* Avoid assigning the same value to a variable back-to-back.Ben Pfaff2012-05-141-1/+0
| | | | | | Found by clang. Signed-off-by: Ben Pfaff <blp@nicira.com>
* Global replace of Nicira Networks.Raju Subramanian2012-05-021-1/+1
| | | | | | | | Replaced all instances of Nicira Networks(, Inc) to Nicira, Inc. Feature #10593 Signed-off-by: Raju Subramanian <rsubramanian@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: New function bitwise_is_all_zeros().Ben Pfaff2012-04-131-0/+47
| | | | Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: New function bitwise_one().Ben Pfaff2012-04-131-0/+43
| | | | | | It's the obvious counterpart to bitwise_zero(). Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: New function bitwise_zero().Ben Pfaff2012-02-011-0/+43
| | | | Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: Move bitwise_copy() here, add new bitwise functions, add a test.Ben Pfaff2012-02-011-1/+61
| | | | | | | | | bitwise_copy() is generally useful so make it a general utility function. Also add a comment. Upcoming commits will introduce users for the new functions. Signed-off-by: Ben Pfaff <blp@nicira.com>
* packets: Add more utility functions for IPv4 and IPv6 addresses.Ben Pfaff2011-09-131-6/+25
| | | | | | | We had these functions scattered around the source tree anyway. packets.h is a good place to centralize them. I do plan to introduce some additional callers.
* util: New function log_2_floor().Ben Pfaff2011-07-221-0/+52
Calculates the position of the most significant bit in a 32 bit word.