kunit: Add dynamically-extending log
v6 cover kunit: Add dynamically-extending log
This patch chain changes the logging implementation to use string_stream so that the log will grow dynamically.
The first 8 patches add test code for string_stream, and make some changes to string_stream needed to be able to use it for the log.
The final patch adds a performance report of string_stream.
CHANGES SINCE V5:
Patch 2:
- Avoid cast warning when using KUNIT_EXPECT_EQ() on a gfp_t. Instead pass the result of the comparison to KUNIT_EXPECT_TRUE(). While it would be nice to use KUNIT_EXPECT_EQ(), it's probably better to avoid introducing build or sparse warnings.
- In string_stream_append_test() rename original_content to stream1_content_before_append.
Patch 7:
- Make string_stream_clear() public (in v5 this was done in patch #8).
- In string-stream-test.c add a wrapper for kfree() to prevent a cast warning when calling kunit_add_action().
Patch 8:
- Fix memory leak when calling the redirected string_stream_destroy_stub().
Patch 9:
- In kunit-test.c: add wrapper function around kfree() to prevent cast warning when calling kunit_add_action().
- Fix unused variable warning in kunit_log_test() when built as a module.
Richard Fitzgerald (10):
kunit: string-stream: Don't create a fragment for empty strings
kunit: string-stream: Improve testing of string_stream
kunit: string-stream: Add option to make all lines end with newline
kunit: string-stream-test: Add cases for string_stream newline
appending
kunit: Don't use a managed alloc in is_literal()
kunit: string-stream: Add kunit_alloc_string_stream()
kunit: string-stream: Decouple string_stream from kunit
kunit: string-stream: Add tests for freeing resource-managed
string_stream
kunit: Use string_stream for test log
kunit: string-stream: Test performance of string_stream
include/kunit/test.h | 14 +- lib/kunit/assert.c | 14 +- lib/kunit/debugfs.c | 36 ++- lib/kunit/kunit-test.c | 56 +++- lib/kunit/string-stream-test.c | 525 +++++++++++++++++++++++++++++++-- lib/kunit/string-stream.c | 100 +++++-- lib/kunit/string-stream.h | 16 +- lib/kunit/test.c | 50 +--- 8 files changed, 688 insertions(+), 123 deletions(-)
Signature · 1 line
2.30.2
PATCH v6 1/10 kunit: string-stream: Don't create a fragment for empty strings
If the result of the formatted string is an empty string just return instead of creating an empty fragment.
--- lib/kunit/string-stream.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
lib/kunit/string-stream.c +8 -2
@@ -50,11 +50,17 @@ int string_stream_vadd(struct string_stream *stream,/* Make a copy because `vsnprintf` could change it */ va_copy (args_for_counting, args); -/* Need space for null byte. */ - len =vsnprintf (NULL ,0 , fmt, args_for_counting) +1 ; +/* Evaluate length of formatted string */ + len =vsnprintf (NULL ,0 , fmt, args_for_counting);va_end (args_for_counting); +if (len ==0 ) +return 0 ; + +/* Need space for null byte. */ + len++; + frag_container =alloc_string_stream_fragment (stream->test, len, stream->gfp);
Signature · 1 line
2.30.2
PATCH v6 2/10 kunit: string-stream: Improve testing of string_stream
In reply to Richard FitzgeraldReplace the minimal tests with more-thorough testing.
string_stream_init_test() tests that struct string_stream is initialized correctly.
string_stream_line_add_test() adds a series of numbered lines and checks that the resulting string contains all the lines.
string_stream_variable_length_line_test() adds a large number of lines of varying length to create many fragments, then tests that all lines are present.
string_stream_append_test() tests various cases of using string_stream_append() to append the content of one stream to another.
Adds string_stream_append_empty_string_test() to test that adding an empty string to a string_stream doesn't create a new empty fragment.
---
Changes since v5:
- Avoid cast warning when using KUNIT_EXPECT_EQ() on a gfp_t. Instead pass the result of the comparison to KUNIT_EXPECT_TRUE(). While it would be nice to use KUNIT_EXPECT_EQ(), it's probably better to avoid introducing build or sparse warnings.
- In string_stream_append_test() rename original_content to stream1_content_before_append.
--- lib/kunit/string-stream-test.c | 233 ++++++++++++++++++++++++++++++--- 1 file changed, 217 insertions(+), 16 deletions(-)
lib/kunit/string-stream-test.c +217 -16
@@ -11,38 +11,239 @@#include "string-stream.h" -static void string_stream_test_empty_on_creation (struct kunit *test) +static char *get_concatenated_string (struct kunit *test,struct string_stream *stream) { -struct string_stream *stream =alloc_string_stream (test,GFP_KERNEL ); +char *str =string_stream_get_string (stream); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, str); + +return str; +} + +/* string_stream object is initialized correctly. */ +static void string_stream_init_test (struct kunit *test) +{ +struct string_stream *stream; + + stream =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); + +KUNIT_EXPECT_EQ (test, stream->length,0 ); +KUNIT_EXPECT_TRUE (test,list_empty (&stream->fragments)); +KUNIT_EXPECT_PTR_EQ (test, stream->test, test); +KUNIT_EXPECT_TRUE (test, (stream->gfp ==GFP_KERNEL ));KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); } -static void string_stream_test_not_empty_after_add (struct kunit *test) +/* +* Add a series of lines to a string_stream. Check that all lines +* appear in the correct order and no characters are dropped. +*/ +static void string_stream_line_add_test (struct kunit *test) { -struct string_stream *stream =alloc_string_stream (test,GFP_KERNEL ); +struct string_stream *stream; +char line[60 ]; +char *concat_string, *pos, *string_end; +size_t len, total_len; +int num_lines, i; -string_stream_add (stream,"Foo" ); + stream =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); -KUNIT_EXPECT_FALSE (test,string_stream_is_empty (stream)); +/* Add series of sequence numbered lines */ + total_len =0 ; +for (i =0 ; i <100 ; ++i) { + len =snprintf (line,sizeof (line), +"The quick brown fox jumps over the lazy penguin %d\n" , i); + +/* Sanity-check that our test string isn't truncated */ +KUNIT_ASSERT_LT (test, len,sizeof (line)); + +string_stream_add (stream, line); + total_len += len; + } + num_lines = i; + + concat_string =get_concatenated_string (test, stream); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, concat_string); +KUNIT_EXPECT_EQ (test,strlen (concat_string), total_len); + +/* +* Split the concatenated string at the newlines and check that +* all the original added strings are present. +*/ + pos = concat_string; +for (i =0 ; i < num_lines; ++i) { + string_end =strchr (pos,'\n' ); +KUNIT_EXPECT_NOT_NULL (test, string_end); + +/* Convert to NULL-terminated string */ + *string_end ='\0' ; + +snprintf (line,sizeof (line), +"The quick brown fox jumps over the lazy penguin %d" , i); +KUNIT_EXPECT_STREQ (test, pos, line); + + pos = string_end +1 ; + } + +/* There shouldn't be any more data after this */ +KUNIT_EXPECT_EQ (test,strlen (pos),0 ); } -static void string_stream_test_get_string (struct kunit *test) +/* Add a series of lines of variable length to a string_stream. */ +static void string_stream_variable_length_line_test (struct kunit *test) { -struct string_stream *stream =alloc_string_stream (test,GFP_KERNEL ); -char *output; +static const char line[] = +"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +" 0123456789!$%^&*()_-+={}[]:;@'~#<>,.?/|" ; +struct string_stream *stream; +struct rnd_state rnd; +char *concat_string, *pos, *string_end; +size_t offset, total_len; +int num_lines, i; -string_stream_add (stream,"Foo" ); -string_stream_add (stream," %s" ,"bar" ); + stream =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); - output =string_stream_get_string (stream); -KUNIT_ASSERT_STREQ (test, output,"Foo bar" ); +/* +* Log many lines of varying lengths until we have created +* many fragments. +* The "randomness" must be repeatable. +*/ +prandom_seed_state (&rnd,3141592653589793238ULL ); + total_len =0 ; +for (i =0 ; i <100 ; ++i) { + offset =prandom_u32_state (&rnd) % (sizeof (line) -1 ); +string_stream_add (stream,"%s\n" , &line[offset]); + total_len +=sizeof (line) - offset; + } + num_lines = i; + + concat_string =get_concatenated_string (test, stream); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, concat_string); +KUNIT_EXPECT_EQ (test,strlen (concat_string), total_len); + +/* +* Split the concatenated string at the newlines and check that +* all the original added strings are present. +*/ +prandom_seed_state (&rnd,3141592653589793238ULL ); + pos = concat_string; +for (i =0 ; i < num_lines; ++i) { + string_end =strchr (pos,'\n' ); +KUNIT_EXPECT_NOT_NULL (test, string_end); + +/* Convert to NULL-terminated string */ + *string_end ='\0' ; + + offset =prandom_u32_state (&rnd) % (sizeof (line) -1 ); +KUNIT_EXPECT_STREQ (test, pos, &line[offset]); + + pos = string_end +1 ; + } + +/* There shouldn't be any more data after this */ +KUNIT_EXPECT_EQ (test,strlen (pos),0 ); +} + +/* Appending the content of one string stream to another. */ +static void string_stream_append_test (struct kunit *test) +{ +static const char *const strings_1[] = { +"one" ,"two" ,"three" ,"four" ,"five" ,"six" , +"seven" ,"eight" ,"nine" ,"ten" , + }; +static const char *const strings_2[] = { +"Apple" ,"Pear" ,"Orange" ,"Banana" ,"Grape" ,"Apricot" , + }; +struct string_stream *stream_1, *stream_2; +const char *stream1_content_before_append, *stream_2_content; +char *combined_content; +size_t combined_length; +int i; + + stream_1 =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_1); + + stream_2 =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_2); + +/* Append content of empty stream to empty stream */ +string_stream_append (stream_1, stream_2); +KUNIT_EXPECT_EQ (test,strlen (get_concatenated_string (test, stream_1)),0 ); + +/* Add some data to stream_1 */ +for (i =0 ; i <ARRAY_SIZE (strings_1); ++i) +string_stream_add (stream_1,"%s\n" , strings_1[i]); + + stream1_content_before_append =get_concatenated_string (test, stream_1); + +/* Append content of empty stream to non-empty stream */ +string_stream_append (stream_1, stream_2); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), + stream1_content_before_append); + +/* Add some data to stream_2 */ +for (i =0 ; i <ARRAY_SIZE (strings_2); ++i) +string_stream_add (stream_2,"%s\n" , strings_2[i]); + +/* Append content of non-empty stream to non-empty stream */ +string_stream_append (stream_1, stream_2); + +/* +* End result should be the original content of stream_1 plus +* the content of stream_2. +*/ + stream_2_content =get_concatenated_string (test, stream_2); + combined_length =strlen (stream1_content_before_append) +strlen (stream_2_content); + combined_length++;/* for terminating \0 */ + combined_content =kunit_kmalloc (test, combined_length,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, combined_content); +snprintf (combined_content, combined_length,"%s%s" , + stream1_content_before_append, stream_2_content); + +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), combined_content); + +/* Append content of non-empty stream to empty stream */ +string_stream_destroy (stream_1); + + stream_1 =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_1); + +string_stream_append (stream_1, stream_2); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), stream_2_content); +} + +/* Adding an empty string should not create a fragment. */ +static void string_stream_append_empty_string_test (struct kunit *test) +{ +struct string_stream *stream; +int original_frag_count; + + stream =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); + +/* Formatted empty string */ +string_stream_add (stream,"%s" ,"" ); +KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); +KUNIT_EXPECT_TRUE (test,list_empty (&stream->fragments)); + +/* Adding an empty string to a non-empty stream */ +string_stream_add (stream,"Add this line" ); + original_frag_count =list_count_nodes (&stream->fragments); + +string_stream_add (stream,"%s" ,"" ); +KUNIT_EXPECT_EQ (test,list_count_nodes (&stream->fragments), original_frag_count); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream),"Add this line" ); }static struct kunit_case string_stream_test_cases[] = { -KUNIT_CASE (string_stream_test_empty_on_creation), -KUNIT_CASE (string_stream_test_not_empty_after_add), -KUNIT_CASE (string_stream_test_get_string), +KUNIT_CASE (string_stream_init_test), +KUNIT_CASE (string_stream_line_add_test), +KUNIT_CASE (string_stream_variable_length_line_test), +KUNIT_CASE (string_stream_append_test), +KUNIT_CASE (string_stream_append_empty_string_test), {} };
Signature · 1 line
2.30.2
PATCH v6 3/10 kunit: string-stream: Add option to make all lines end with newline
In reply to Richard FitzgeraldAdd an optional feature to string_stream that will append a newline to any added string that does not already end with a newline. The purpose of this is so that string_stream can be used to collect log lines.
This is enabled/disabled by calling string_stream_set_append_newlines().
--- lib/kunit/string-stream.c | 28 +++++++++++++++++++++------- lib/kunit/string-stream.h | 7 +++++++ 2 files changed, 28 insertions(+), 7 deletions(-)
lib/kunit/string-stream.c +21 -7
@@ -44,32 +44,46 @@ int string_stream_vadd(struct string_stream *stream, va_list args) {struct string_stream_fragment *frag_container; -int len; +int buf_len, result_len;va_list args_for_counting;/* Make a copy because `vsnprintf` could change it */ va_copy (args_for_counting, args);/* Evaluate length of formatted string */ - len =vsnprintf (NULL ,0 , fmt, args_for_counting); + buf_len =vsnprintf (NULL ,0 , fmt, args_for_counting);va_end (args_for_counting); -if (len ==0 ) +if (buf_len ==0 )return 0 ; +/* Reserve one extra for possible appended newline. */ +if (stream->append_newlines) + buf_len++; +/* Need space for null byte. */ - len++; + buf_len++; frag_container =alloc_string_stream_fragment (stream->test, - len, + buf_len, stream->gfp);if (IS_ERR (frag_container))return PTR_ERR (frag_container); - len =vsnprintf (frag_container->fragment, len, fmt, args); +if (stream->append_newlines) { +/* Don't include reserved newline byte in writeable length. */ + result_len =vsnprintf (frag_container->fragment, buf_len -1 , fmt, args); + +/* Append newline if necessary. */ +if (frag_container->fragment[result_len -1 ] !='\n' ) + result_len =strlcat (frag_container->fragment,"\n" , buf_len); + }else { + result_len =vsnprintf (frag_container->fragment, buf_len, fmt, args); + } +spin_lock (&stream->lock); - stream->length += len; + stream->length += result_len;list_add_tail (&frag_container->node, &stream->fragments);spin_unlock (&stream->lock);
lib/kunit/string-stream.h +7 -0
@@ -25,6 +25,7 @@ struct string_stream {spinlock_t lock;struct kunit *test;gfp_t gfp; +bool append_newlines; };struct kunit ;
@@ -47,4 +48,10 @@ bool string_stream_is_empty(struct string_stream *stream);void string_stream_destroy (struct string_stream *stream); +static inline void string_stream_set_append_newlines (struct string_stream *stream, +bool append_newlines) +{ + stream->append_newlines = append_newlines; +} +#endif /* _KUNIT_STRING_STREAM_H */
Signature · 1 line
2.30.2
PATCH v6 4/10 kunit: string-stream-test: Add cases for string_stream newline appending
In reply to Richard FitzgeraldAdd test cases for testing the string_stream feature that appends a newline to strings that do not already end with a newline.
string_stream_no_auto_newline_test() tests with this feature disabled. Newlines should not be added or dropped.
string_stream_auto_newline_test() tests with this feature enabled. Newlines should be added to lines that do not end with a newline.
string_stream_append_auto_newline_test() tests appending the content of one stream to another stream when the target stream has newline appending enabled.
--- lib/kunit/string-stream-test.c | 93 ++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+)
lib/kunit/string-stream-test.c +93 -0
@@ -32,6 +32,7 @@ static void string_stream_init_test(struct kunit *test)KUNIT_EXPECT_TRUE (test,list_empty (&stream->fragments));KUNIT_EXPECT_PTR_EQ (test, stream->test, test);KUNIT_EXPECT_TRUE (test, (stream->gfp ==GFP_KERNEL )); +KUNIT_EXPECT_FALSE (test, stream->append_newlines);KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); }
@@ -215,6 +216,45 @@ static void string_stream_append_test(struct kunit *test)KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), stream_2_content); } +/* Appending the content of one string stream to one with auto-newlining. */ +static void string_stream_append_auto_newline_test (struct kunit *test) +{ +struct string_stream *stream_1, *stream_2; + +/* Stream 1 has newline appending enabled */ + stream_1 =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_1); +string_stream_set_append_newlines (stream_1, true); +KUNIT_EXPECT_TRUE (test, stream_1->append_newlines); + +/* Stream 2 does not append newlines */ + stream_2 =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_2); + +/* Appending a stream with a newline should not add another newline */ +string_stream_add (stream_1,"Original string\n" ); +string_stream_add (stream_2,"Appended content\n" ); +string_stream_add (stream_2,"More stuff\n" ); +string_stream_append (stream_1, stream_2); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), +"Original string\nAppended content\nMore stuff\n" ); + +string_stream_destroy (stream_2); + stream_2 =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_2); + +/* +* Appending a stream without newline should add a final newline. +* The appended string_stream is treated as a single string so newlines +* should not be inserted between fragments. +*/ +string_stream_add (stream_2,"Another" ); +string_stream_add (stream_2,"And again" ); +string_stream_append (stream_1, stream_2); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), +"Original string\nAppended content\nMore stuff\nAnotherAnd again\n" ); +} +/* Adding an empty string should not create a fragment. */ static void string_stream_append_empty_string_test (struct kunit *test) {
@@ -238,12 +278,65 @@ static void string_stream_append_empty_string_test(struct kunit *test)KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream),"Add this line" ); } +/* Adding strings without automatic newline appending */ +static void string_stream_no_auto_newline_test (struct kunit *test) +{ +struct string_stream *stream; + + stream =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); + +/* +* Add some strings with and without newlines. All formatted newlines +* should be preserved. It should not add any extra newlines. +*/ +string_stream_add (stream,"One" ); +string_stream_add (stream,"Two\n" ); +string_stream_add (stream,"%s\n" ,"Three" ); +string_stream_add (stream,"%s" ,"Four\n" ); +string_stream_add (stream,"Five\n%s" ,"Six" ); +string_stream_add (stream,"Seven\n\n" ); +string_stream_add (stream,"Eight" ); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream), +"OneTwo\nThree\nFour\nFive\nSixSeven\n\nEight" ); +} + +/* Adding strings with automatic newline appending */ +static void string_stream_auto_newline_test (struct kunit *test) +{ +struct string_stream *stream; + + stream =alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); + +string_stream_set_append_newlines (stream, true); +KUNIT_EXPECT_TRUE (test, stream->append_newlines); + +/* +* Add some strings with and without newlines. Newlines should +* be appended to lines that do not end with \n, but newlines +* resulting from the formatting should not be changed. +*/ +string_stream_add (stream,"One" ); +string_stream_add (stream,"Two\n" ); +string_stream_add (stream,"%s\n" ,"Three" ); +string_stream_add (stream,"%s" ,"Four\n" ); +string_stream_add (stream,"Five\n%s" ,"Six" ); +string_stream_add (stream,"Seven\n\n" ); +string_stream_add (stream,"Eight" ); +KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream), +"One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n" ); +} +static struct kunit_case string_stream_test_cases[] = {KUNIT_CASE (string_stream_init_test),KUNIT_CASE (string_stream_line_add_test),KUNIT_CASE (string_stream_variable_length_line_test),KUNIT_CASE (string_stream_append_test), +KUNIT_CASE (string_stream_append_auto_newline_test),KUNIT_CASE (string_stream_append_empty_string_test), +KUNIT_CASE (string_stream_no_auto_newline_test), +KUNIT_CASE (string_stream_auto_newline_test), {} };
Signature · 1 line
2.30.2
PATCH v6 5/10 kunit: Don't use a managed alloc in is_literal()
In reply to Richard FitzgeraldThere is no need to use a test-managed alloc in is_literal(). The function frees the temporary buffer before returning.
This removes the only use of the test and gfp members of struct string_stream outside of the string_stream implementation.
--- lib/kunit/assert.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
lib/kunit/assert.c +6 -8
@@ -89,8 +89,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,EXPORT_SYMBOL_GPL (kunit_ptr_not_err_assert_format);/* Checks if `text` is a literal representing `value`, e.g. "5" and 5 */ -static bool is_literal (struct kunit *test,const char *text,long long value, -gfp_t gfp) +static bool is_literal (const char *text,long long value) {char *buffer;int len;
@@ -100,14 +99,15 @@ static bool is_literal(struct kunit *test, const char *text, long long value,if (strlen (text) != len)return false; - buffer =kunit_kmalloc (test, len+1 , gfp); + buffer =kmalloc (len+1 ,GFP_KERNEL );if (!buffer)return false;snprintf (buffer, len+1 ,"%lld" , value); ret =strncmp (buffer, text, len) ==0 ; -kunit_kfree (test, buffer); +kfree (buffer); +return ret; }
@@ -125,14 +125,12 @@ void kunit_binary_assert_format(const struct kunit_assert *assert, binary_assert->text->left_text, binary_assert->text->operation, binary_assert->text->right_text); -if (!is_literal (stream->test, binary_assert->text->left_text, - binary_assert->left_value, stream->gfp)) +if (!is_literal (binary_assert->text->left_text, binary_assert->left_value))string_stream_add (stream,KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n" , binary_assert->text->left_text, binary_assert->left_value, binary_assert->left_value); -if (!is_literal (stream->test, binary_assert->text->right_text, - binary_assert->right_value, stream->gfp)) +if (!is_literal (binary_assert->text->right_text, binary_assert->right_value)) string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)", binary_assert->text->right_text, binary_assert->right_value,
Signature · 1 line
2.30.2
PATCH v6 6/10 kunit: string-stream: Add kunit_alloc_string_stream()
In reply to Richard FitzgeraldAdd function kunit_alloc_string_stream() to do a resource-managed allocation of a string stream, and corresponding kunit_free_string_stream() to free the resource-managed stream.
This is preparing for decoupling the string_stream implementation from struct kunit, to reduce the amount of code churn when that happens. Currently:
- kunit_alloc_string_stream() only calls alloc_string_stream().
- kunit_free_string_stream() takes a struct kunit* which isn't used yet.
Callers of the old alloc_string_stream() and string_stream_destroy() are all requesting a managed allocation so have been changed to use the new functions.
alloc_string_stream() has been temporarily made static because its current behavior has been replaced with kunit_alloc_string_stream().
--- lib/kunit/string-stream-test.c | 28 ++++++++++++++-------------- lib/kunit/string-stream.c | 12 +++++++++++- lib/kunit/string-stream.h | 3 ++- lib/kunit/test.c | 4 ++-- 4 files changed, 29 insertions(+), 18 deletions(-)
lib/kunit/string-stream-test.c +14 -14
@@ -25,7 +25,7 @@ static void string_stream_init_test(struct kunit *test) {struct string_stream *stream; - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);KUNIT_EXPECT_EQ (test, stream->length,0 );
@@ -48,7 +48,7 @@ static void string_stream_line_add_test(struct kunit *test)size_t len, total_len;int num_lines, i; - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);/* Add series of sequence numbered lines */
@@ -104,7 +104,7 @@ static void string_stream_variable_length_line_test(struct kunit *test)size_t offset, total_len;int num_lines, i; - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);/*
@@ -164,10 +164,10 @@ static void string_stream_append_test(struct kunit *test)size_t combined_length;int i; - stream_1 =alloc_string_stream (test,GFP_KERNEL ); + stream_1 =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_1); - stream_2 =alloc_string_stream (test,GFP_KERNEL ); + stream_2 =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_2);/* Append content of empty stream to empty stream */
@@ -207,9 +207,9 @@ static void string_stream_append_test(struct kunit *test)KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1), combined_content);/* Append content of non-empty stream to empty stream */ -string_stream_destroy (stream_1); +kunit_free_string_stream (test, stream_1); - stream_1 =alloc_string_stream (test,GFP_KERNEL ); + stream_1 =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_1);string_stream_append (stream_1, stream_2);
@@ -222,13 +222,13 @@ static void string_stream_append_auto_newline_test(struct kunit *test)struct string_stream *stream_1, *stream_2;/* Stream 1 has newline appending enabled */ - stream_1 =alloc_string_stream (test,GFP_KERNEL ); + stream_1 =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_1);string_stream_set_append_newlines (stream_1, true);KUNIT_EXPECT_TRUE (test, stream_1->append_newlines);/* Stream 2 does not append newlines */ - stream_2 =alloc_string_stream (test,GFP_KERNEL ); + stream_2 =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_2);/* Appending a stream with a newline should not add another newline */
@@ -239,8 +239,8 @@ static void string_stream_append_auto_newline_test(struct kunit *test)KUNIT_EXPECT_STREQ (test,get_concatenated_string (test, stream_1),"Original string\nAppended content\nMore stuff\n" ); -string_stream_destroy (stream_2); - stream_2 =alloc_string_stream (test,GFP_KERNEL ); +kunit_free_string_stream (test, stream_2); + stream_2 =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream_2);/*
@@ -261,7 +261,7 @@ static void string_stream_append_empty_string_test(struct kunit *test)struct string_stream *stream;int original_frag_count; - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);/* Formatted empty string */
@@ -283,7 +283,7 @@ static void string_stream_no_auto_newline_test(struct kunit *test) {struct string_stream *stream; - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);/*
@@ -306,7 +306,7 @@ static void string_stream_auto_newline_test(struct kunit *test) {struct string_stream *stream; - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);string_stream_set_append_newlines (stream, true);
lib/kunit/string-stream.c +11 -1
@@ -153,7 +153,7 @@ bool string_stream_is_empty(struct string_stream *stream)return list_empty (&stream->fragments); } -struct string_stream *alloc_string_stream (struct kunit *test,gfp_t gfp) +static struct string_stream *alloc_string_stream (struct kunit *test,gfp_t gfp) {struct string_stream *stream;
@@ -173,3 +173,13 @@ void string_stream_destroy(struct string_stream *stream) {string_stream_clear (stream); } + +struct string_stream *kunit_alloc_string_stream (struct kunit *test,gfp_t gfp) +{ +return alloc_string_stream (test, gfp); +} + +void kunit_free_string_stream (struct kunit *test,struct string_stream *stream) +{ +string_stream_destroy (stream); +}
lib/kunit/string-stream.h +2 -1
@@ -30,7 +30,8 @@ struct string_stream {struct kunit ; -struct string_stream *alloc_string_stream (struct kunit *test,gfp_t gfp); +struct string_stream *kunit_alloc_string_stream (struct kunit *test,gfp_t gfp); +void kunit_free_string_stream (struct kunit *test,struct string_stream *stream); int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...);
lib/kunit/test.c +2 -2
@@ -308,7 +308,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,kunit_set_failure (test); - stream =alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_KERNEL );if (IS_ERR (stream)) { WARN(true, "Could not allocate stream to print failed assertion in %s:%d\n",
@@ -322,7 +322,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,kunit_print_string_stream (test, stream); -string_stream_destroy (stream); +kunit_free_string_stream (test, stream); }void __noreturn __kunit_abort (struct kunit *test)
Signature · 1 line
2.30.2
PATCH v6 7/10 kunit: string-stream: Decouple string_stream from kunit
In reply to Richard FitzgeraldRe-work string_stream so that it is not tied to a struct kunit. This is to allow using it for the log of struct kunit_suite.
Instead of resource-managing individual allocations the whole string_stream can be resource-managed, if required.
alloc_string_stream() now allocates a string stream that is not resource-managed.
string_stream_destroy() now works on an unmanaged string_stream allocated by alloc_string_stream() and frees the entire string_stream (previously it only freed the fragments).
string_stream_clear() has been made public for callers that want to free the fragments without destroying the string_stream.
For resource-managed allocations use kunit_alloc_string_stream() and kunit_free_string_stream().
In addition to this, string_stream_get_string() now returns an unmanaged buffer that the caller must kfree().
---
Changes since v5:
- Make string_stream_clear() public (in v5 this was done in patch #8).
- In string-stream-test.c add a wrapper for kfree() to prevent a cast warning when calling kunit_add_action().
--- lib/kunit/string-stream-test.c | 8 ++++- lib/kunit/string-stream.c | 61 ++++++++++++++++++++++------------ lib/kunit/string-stream.h | 6 +++- lib/kunit/test.c | 2 +- 4 files changed, 53 insertions(+), 24 deletions(-)
lib/kunit/string-stream-test.c +7 -1
@@ -11,11 +11,18 @@#include "string-stream.h" +/* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ +static void kfree_wrapper (void *p) +{ +kfree (p); +} +static char *get_concatenated_string (struct kunit *test,struct string_stream *stream) {char *str =string_stream_get_string (stream);KUNIT_ASSERT_NOT_ERR_OR_NULL (test, str); +kunit_add_action (test, kfree_wrapper, (void *)str);return str; }
@@ -30,7 +37,6 @@ static void string_stream_init_test(struct kunit *test)KUNIT_EXPECT_EQ (test, stream->length,0 );KUNIT_EXPECT_TRUE (test,list_empty (&stream->fragments)); -KUNIT_EXPECT_PTR_EQ (test, stream->test, test);KUNIT_EXPECT_TRUE (test, (stream->gfp ==GFP_KERNEL ));KUNIT_EXPECT_FALSE (test, stream->append_newlines);KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream));
lib/kunit/string-stream.c +40 -21
144 lines not on this page · raw message
lib/kunit/string-stream.h +5 -1
@@ -23,7 +23,6 @@ struct string_stream {struct list_head fragments;/* length and fragments are protected by this lock */ spinlock_t lock; -struct kunit *test;gfp_t gfp;bool append_newlines; };
@@ -33,6 +32,9 @@ struct kunit;struct string_stream *kunit_alloc_string_stream (struct kunit *test,gfp_t gfp);void kunit_free_string_stream (struct kunit *test,struct string_stream *stream); +struct string_stream *alloc_string_stream (gfp_t gfp); +void free_string_stream (struct string_stream *stream); + int __printf(2, 3) string_stream_add(struct string_stream *stream, const char *fmt, ...);
@@ -40,6 +42,8 @@ int __printf(2, 0) string_stream_vadd(struct string_stream *stream,const char *fmt, va_list args); +void string_stream_clear (struct string_stream *stream); +char *string_stream_get_string (struct string_stream *stream); int string_stream_append(struct string_stream *stream,
lib/kunit/test.c +1 -1
@@ -296,7 +296,7 @@ static void kunit_print_string_stream(struct kunit *test,kunit_err (test,"\n" ); }else {kunit_err (test,"%s" , buf); -kunit_kfree (test, buf); +kfree (buf); } }
Signature · 1 line
2.30.2
PATCH v6 8/10 kunit: string-stream: Add tests for freeing resource-managed string_stream
In reply to Richard Fitzgeraldstring_stream_managed_free_test() allocates a resource-managed string_stream and tests that kunit_free_string_stream() calls string_stream_destroy().
string_stream_resource_free_test() allocates a resource-managed string_stream and tests that string_stream_destroy() is called when the test resources are cleaned up.
The old string_stream_init_test() has been split into two tests, one for kunit_alloc_string_stream() and the other for alloc_string_stream().
---
Changes since v5:
- Fix memory leak when calling the redirected string_stream_destroy_stub().
--- lib/kunit/string-stream-test.c | 147 +++++++++++++++++++++++++++++++-- lib/kunit/string-stream.c | 3 + 2 files changed, 145 insertions(+), 5 deletions(-)
lib/kunit/string-stream-test.c +142 -5
@@ -6,17 +6,33 @@* Author: Brendan Higgins <[email protected]> */ +#include <kunit/static_stub.h> #include <kunit/test.h> #include <linux/slab.h> #include "string-stream.h" -/* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ +struct string_stream_test_priv { +/* For testing resource-managed free. */ +struct string_stream *expected_free_stream; +bool stream_was_freed; +bool stream_free_again; +}; + +/* Avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ static void kfree_wrapper (void *p) {kfree (p); } +/* Avoids a cast warning if string_stream_destroy() is passed direct to kunit_add_action(). */ +static void cleanup_raw_stream (void *p) +{ +struct string_stream *stream = p; + +string_stream_destroy (stream); +} +static char *get_concatenated_string (struct kunit *test,struct string_stream *stream) {char *str =string_stream_get_string (stream);
@@ -27,11 +43,12 @@ static char *get_concatenated_string(struct kunit *test, struct string_stream *sreturn str; } -/* string_stream object is initialized correctly. */ -static void string_stream_init_test (struct kunit *test) +/* Managed string_stream object is initialized correctly. */ +static void string_stream_managed_init_test (struct kunit *test) {struct string_stream *stream; +/* Resource-managed initialization. */ stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);
@@ -42,6 +59,109 @@ static void string_stream_init_test(struct kunit *test)KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); } +/* Unmanaged string_stream object is initialized correctly. */ +static void string_stream_unmanaged_init_test (struct kunit *test) +{ +struct string_stream *stream; + + stream =alloc_string_stream (GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); +kunit_add_action (test, cleanup_raw_stream, stream); + +KUNIT_EXPECT_EQ (test, stream->length,0 ); +KUNIT_EXPECT_TRUE (test,list_empty (&stream->fragments)); +KUNIT_EXPECT_EQ (test, stream->gfp,GFP_KERNEL ); +KUNIT_EXPECT_FALSE (test, stream->append_newlines); + +KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); +} + +static void string_stream_destroy_stub (struct string_stream *stream) +{ +struct kunit *fake_test =kunit_get_current_test (); +struct string_stream_test_priv *priv = fake_test->priv; + +/* The kunit could own string_streams other than the one we are testing. */ +if (stream == priv->expected_free_stream) { +if (priv->stream_was_freed) + priv->stream_free_again = true; +else + priv->stream_was_freed = true; + } + +/* +* Calling string_stream_destroy() will only call this function again +* because the redirection stub is still active. +* Avoid calling deactivate_static_stub() or changing current->kunit_test +* during cleanup. +*/ +string_stream_clear (stream); +kfree (stream); +} + +/* kunit_free_string_stream() calls string_stream_desrtoy() */ +static void string_stream_managed_free_test (struct kunit *test) +{ +struct string_stream_test_priv *priv = test->priv; + + priv->expected_free_stream =NULL ; + priv->stream_was_freed = false; + priv->stream_free_again = false; + +kunit_activate_static_stub (test, + string_stream_destroy, + string_stream_destroy_stub); + + priv->expected_free_stream =kunit_alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv->expected_free_stream); + +/* This should call the stub function. */ +kunit_free_string_stream (test, priv->expected_free_stream); + +KUNIT_EXPECT_TRUE (test, priv->stream_was_freed); +KUNIT_EXPECT_FALSE (test, priv->stream_free_again); +} + +/* string_stream object is freed when test is cleaned up. */ +static void string_stream_resource_free_test (struct kunit *test) +{ +struct string_stream_test_priv *priv = test->priv; +struct kunit *fake_test; + + fake_test =kunit_kzalloc (test,sizeof (*fake_test),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fake_test); + +kunit_init_test (fake_test,"string_stream_fake_test" ,NULL ); + fake_test->priv = priv; + +/* +* Activate stub before creating string_stream so the +* string_stream will be cleaned up first. +*/ + priv->expected_free_stream =NULL ; + priv->stream_was_freed = false; + priv->stream_free_again = false; + +kunit_activate_static_stub (fake_test, + string_stream_destroy, + string_stream_destroy_stub); + + priv->expected_free_stream =kunit_alloc_string_stream (fake_test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv->expected_free_stream); + +/* Set current->kunit_test to fake_test so the static stub will be called. */ + current->kunit_test = fake_test; + +/* Cleanup test - the stub function should be called */ +kunit_cleanup (fake_test); + +/* Set current->kunit_test back to current test. */ + current->kunit_test = test; + +KUNIT_EXPECT_TRUE (test, priv->stream_was_freed); +KUNIT_EXPECT_FALSE (test, priv->stream_free_again); +} +/* * Add a series of lines to a string_stream. Check that all lines * appear in the correct order and no characters are dropped.
@@ -334,8 +454,24 @@ static void string_stream_auto_newline_test(struct kunit *test) "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); } +static int string_stream_test_init (struct kunit *test) +{ +struct string_stream_test_priv *priv; + + priv =kunit_kzalloc (test,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + test->priv = priv; + +return 0 ; +} +static struct kunit_case string_stream_test_cases[] = { -KUNIT_CASE (string_stream_init_test), +KUNIT_CASE (string_stream_managed_init_test), +KUNIT_CASE (string_stream_unmanaged_init_test), +KUNIT_CASE (string_stream_managed_free_test), +KUNIT_CASE (string_stream_resource_free_test),KUNIT_CASE (string_stream_line_add_test),KUNIT_CASE (string_stream_variable_length_line_test),KUNIT_CASE (string_stream_append_test),
@@ -348,6 +484,7 @@ static struct kunit_case string_stream_test_cases[] = {static struct kunit_suite string_stream_test_suite = { .name ="string-stream-test" , - .test_cases = string_stream_test_cases + .test_cases = string_stream_test_cases, + .init = string_stream_test_init, };kunit_test_suites (&string_stream_test_suite);
lib/kunit/string-stream.c +3 -0
@@ -6,6 +6,7 @@* Author: Brendan Higgins <[email protected]> */ +#include <kunit/static_stub.h> #include <kunit/test.h> #include <linux/list.h> #include <linux/slab.h>
@@ -170,6 +171,8 @@ struct string_stream *alloc_string_stream(gfp_t gfp)void string_stream_destroy (struct string_stream *stream) { +KUNIT_STATIC_STUB_REDIRECT (string_stream_destroy, stream); +if (!stream)return ;
Signature · 1 line
2.30.2
Re: [PATCH v6 08/10] kunit: string-stream: Add tests for freeing resource-managed string_stream
Quoted 13 lines · Richard Fitzgerald
string_stream_managed_free_test()allocates a resource-managedstring_streamand tests thatkunit_free_string_stream()callsstring_stream_destroy().
string_stream_resource_free_test()allocates a resource-managedstring_streamand tests thatstring_stream_destroy()is called when the test resources are cleaned up.The old
string_stream_init_test()has been split into two tests, one forkunit_alloc_string_stream()and the other foralloc_string_stream().
Quoted 5 lines
Signed-off-by Richard Fitzgerald <[email protected]>---
Changes since v5:
- Fix memory leak when calling the redirectedstring_stream_destroy_stub().
---
Much better, thanks.
Cheers,
-- David
Quoted 227 lines
lib/kunit/string-stream-test.c | 147 +++++++++++++++++++++++++++++++-- lib/kunit/string-stream.c | 3 + 2 files changed, 145 insertions(+), 5 deletions(-)lib/kunit/string-stream-test.c +142
-5@@ -6,17 +6,33 @@* Author: Brendan Higgins <[email protected]> */ +#include <kunit/static_stub.h> #include <kunit/test.h> #include <linux/slab.h> #include "string-stream.h" -/* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ +struct string_stream_test_priv { +/* For testing resource-managed free. */ +struct string_stream *expected_free_stream; +bool stream_was_freed; +bool stream_free_again; +}; + +/* Avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ static void kfree_wrapper (void *p) {kfree (p); } +/* Avoids a cast warning if string_stream_destroy() is passed direct to kunit_add_action(). */ +static void cleanup_raw_stream (void *p) +{ +struct string_stream *stream = p; + +string_stream_destroy (stream); +} +static char *get_concatenated_string (struct kunit *test,struct string_stream *stream) {char *str =string_stream_get_string (stream);@@ -27,11 +43,12 @@ static char *get_concatenated_string(struct kunit *test, struct string_stream *sreturn str; } -/* string_stream object is initialized correctly. */ -static void string_stream_init_test (struct kunit *test) +/* Managed string_stream object is initialized correctly. */ +static void string_stream_managed_init_test (struct kunit *test) {struct string_stream *stream; +/* Resource-managed initialization. */ stream =kunit_alloc_string_stream (test,GFP_KERNEL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream);@@ -42,6 +59,109 @@ static void string_stream_init_test(struct kunit *test)KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); } +/* Unmanaged string_stream object is initialized correctly. */ +static void string_stream_unmanaged_init_test (struct kunit *test) +{ +struct string_stream *stream; + + stream =alloc_string_stream (GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); +kunit_add_action (test, cleanup_raw_stream, stream); + +KUNIT_EXPECT_EQ (test, stream->length,0 ); +KUNIT_EXPECT_TRUE (test,list_empty (&stream->fragments)); +KUNIT_EXPECT_EQ (test, stream->gfp,GFP_KERNEL ); +KUNIT_EXPECT_FALSE (test, stream->append_newlines); + +KUNIT_EXPECT_TRUE (test,string_stream_is_empty (stream)); +} + +static void string_stream_destroy_stub (struct string_stream *stream) +{ +struct kunit *fake_test =kunit_get_current_test (); +struct string_stream_test_priv *priv = fake_test->priv; + +/* The kunit could own string_streams other than the one we are testing. */ +if (stream == priv->expected_free_stream) { +if (priv->stream_was_freed) + priv->stream_free_again = true; +else + priv->stream_was_freed = true; + } + +/* +* Calling string_stream_destroy() will only call this function again +* because the redirection stub is still active. +* Avoid calling deactivate_static_stub() or changing current->kunit_test +* during cleanup. +*/ +string_stream_clear (stream); +kfree (stream); +} + +/* kunit_free_string_stream() calls string_stream_desrtoy() */ +static void string_stream_managed_free_test (struct kunit *test) +{ +struct string_stream_test_priv *priv = test->priv; + + priv->expected_free_stream =NULL ; + priv->stream_was_freed = false; + priv->stream_free_again = false; + +kunit_activate_static_stub (test, + string_stream_destroy, + string_stream_destroy_stub); + + priv->expected_free_stream =kunit_alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv->expected_free_stream); + +/* This should call the stub function. */ +kunit_free_string_stream (test, priv->expected_free_stream); + +KUNIT_EXPECT_TRUE (test, priv->stream_was_freed); +KUNIT_EXPECT_FALSE (test, priv->stream_free_again); +} + +/* string_stream object is freed when test is cleaned up. */ +static void string_stream_resource_free_test (struct kunit *test) +{ +struct string_stream_test_priv *priv = test->priv; +struct kunit *fake_test; + + fake_test =kunit_kzalloc (test,sizeof (*fake_test),GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fake_test); + +kunit_init_test (fake_test,"string_stream_fake_test" ,NULL ); + fake_test->priv = priv; + +/* +* Activate stub before creating string_stream so the +* string_stream will be cleaned up first. +*/ + priv->expected_free_stream =NULL ; + priv->stream_was_freed = false; + priv->stream_free_again = false; + +kunit_activate_static_stub (fake_test, + string_stream_destroy, + string_stream_destroy_stub); + + priv->expected_free_stream =kunit_alloc_string_stream (fake_test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, priv->expected_free_stream); + +/* Set current->kunit_test to fake_test so the static stub will be called. */ + current->kunit_test = fake_test; + +/* Cleanup test - the stub function should be called */ +kunit_cleanup (fake_test); + +/* Set current->kunit_test back to current test. */ + current->kunit_test = test; + +KUNIT_EXPECT_TRUE (test, priv->stream_was_freed); +KUNIT_EXPECT_FALSE (test, priv->stream_free_again); +} +/* * Add a series of lines to a string_stream. Check that all lines * appear in the correct order and no characters are dropped. @@ -334,8 +454,24 @@ static void string_stream_auto_newline_test(struct kunit *test) "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); } +static int string_stream_test_init (struct kunit *test) +{ +struct string_stream_test_priv *priv; + + priv =kunit_kzalloc (test,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + test->priv = priv; + +return 0 ; +} +static struct kunit_case string_stream_test_cases[] = { -KUNIT_CASE (string_stream_init_test), +KUNIT_CASE (string_stream_managed_init_test), +KUNIT_CASE (string_stream_unmanaged_init_test), +KUNIT_CASE (string_stream_managed_free_test), +KUNIT_CASE (string_stream_resource_free_test),KUNIT_CASE (string_stream_line_add_test),KUNIT_CASE (string_stream_variable_length_line_test),KUNIT_CASE (string_stream_append_test),@@ -348,6 +484,7 @@ static struct kunit_case string_stream_test_cases[] = {static struct kunit_suite string_stream_test_suite = { .name ="string-stream-test" , - .test_cases = string_stream_test_cases + .test_cases = string_stream_test_cases, + .init = string_stream_test_init, };kunit_test_suites (&string_stream_test_suite);lib/kunit/string-stream.c +3
-1@@ -6,6 +6,7 @@* Author: Brendan Higgins <[email protected]> */ +#include <kunit/static_stub.h> #include <kunit/test.h> #include <linux/list.h> #include <linux/slab.h> @@ -170,6 +171,8 @@ struct string_stream *alloc_string_stream(gfp_t gfp)void string_stream_destroy (struct string_stream *stream) { +KUNIT_STATIC_STUB_REDIRECT (string_stream_destroy, stream); +if (!stream)return ; --2.30.2
PATCH v6 9/10 kunit: Use string_stream for test log
In reply to Richard FitzgeraldReplace the fixed-size log buffer with a string_stream so that the log can grow as lines are added.
The existing kunit log tests have been updated for using a string_stream as the log. No new test have been added because there are already tests for the underlying string_stream.
As the log tests now depend on string_stream functions they cannot build when kunit-test is a module. They have been surrounded by a #if to replace them with skipping version when the test is build as a module. Though this isn't pretty, it avoids moving code to another file while that code is also being changed.
---
Changes since V5:
- In kunit-test.c: add wrapper function around kfree() to prevent cast warning when calling kunit_add_action().
- Fix unused variable warning in kunit_log_test() when built as a module.
--- include/kunit/test.h | 14 +++++------ lib/kunit/debugfs.c | 36 +++++++++++++++++---------- lib/kunit/kunit-test.c | 56 +++++++++++++++++++++++++++++++++++------- lib/kunit/test.c | 44 ++++----------------------------- 4 files changed, 81 insertions(+), 69 deletions(-)
include/kunit/test.h +6 -8
@@ -32,9 +32,7 @@DECLARE_STATIC_KEY_FALSE (kunit_running);struct kunit ; - -/* Size of log associated with test. */ -#define KUNIT_LOG_SIZE 2048 +struct string_stream ;/* Maximum size of parameter description string. */ #define KUNIT_PARAM_DESC_SIZE 128
@@ -132,7 +130,7 @@ struct kunit_case {/* private: internal use only. */ enum kunit_status status;char *module_name; -char *log; +struct string_stream *log; };static inline char *kunit_status_to_ok_not_ok (enum kunit_status status)
@@ -252,7 +250,7 @@ struct kunit_suite {/* private: internal use only */ char status_comment[KUNIT_STATUS_COMMENT_SIZE ];struct dentry *debugfs; -char *log; +struct string_stream *log;int suite_init_err; };
@@ -278,7 +276,7 @@ struct kunit {/* private: internal use only. */ const char *name;/* Read only after initialization! */ -char *log;/* Points at case log after initialization */ +struct string_stream *log;/* Points at case log after initialization */ struct kunit_try_catch try_catch;/* param_value is the current parameter value for a test case. */ const void *param_value;
@@ -314,7 +312,7 @@ const char *kunit_filter_glob(void);char *kunit_filter (void );char *kunit_filter_action (void ); -void kunit_init_test (struct kunit *test,const char *name,char *log); +void kunit_init_test (struct kunit *test,const char *name,struct string_stream *log);int kunit_run_tests (struct kunit_suite *suite);
@@ -472,7 +470,7 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfpvoid kunit_cleanup (struct kunit *test); -void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); +void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...); /** * kunit_mark_skipped() - Marks @test_or_suite as skipped
lib/kunit/debugfs.c +23 -13
@@ -37,14 +37,21 @@ void kunit_debugfs_init(void) debugfs_rootdir =debugfs_create_dir (KUNIT_DEBUGFS_ROOT ,NULL ); } -static void debugfs_print_result (struct seq_file *seq, -struct kunit_suite *suite, -struct kunit_case *test_case) +static void debugfs_print_result (struct seq_file *seq,struct string_stream *log) { -if (!test_case || !test_case->log) +struct string_stream_fragment *frag_container; + +if (!log)return ; -seq_printf (seq,"%s" , test_case->log); +/* +* Walk the fragments so we don't need to allocate a temporary +* buffer to hold the entire string. +*/ +spin_lock (&log->lock); +list_for_each_entry (frag_container, &log->fragments, node) +seq_printf (seq,"%s" , frag_container->fragment); +spin_unlock (&log->lock); }/*
@@ -69,10 +76,9 @@ static int debugfs_print_results(struct seq_file *seq, void *v)seq_printf (seq,KUNIT_SUBTEST_INDENT "1..%zd\n" ,kunit_suite_num_test_cases (suite));kunit_suite_for_each_test_case (suite, test_case) -debugfs_print_result (seq, suite, test_case); +debugfs_print_result (seq, test_case->log); -if (suite->log) -seq_printf (seq,"%s" , suite->log); +debugfs_print_result (seq, suite->log);seq_printf (seq,"%s %d %s\n" ,kunit_status_to_ok_not_ok (success),1 , suite->name);
@@ -105,9 +111,13 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite)struct kunit_case *test_case;/* Allocate logs before creating debugfs representation. */ - suite->log =kzalloc (KUNIT_LOG_SIZE ,GFP_KERNEL ); -kunit_suite_for_each_test_case (suite, test_case) - test_case->log =kzalloc (KUNIT_LOG_SIZE ,GFP_KERNEL ); + suite->log =alloc_string_stream (GFP_KERNEL ); +string_stream_set_append_newlines (suite->log, true); + +kunit_suite_for_each_test_case (suite, test_case) { + test_case->log =alloc_string_stream (GFP_KERNEL ); +string_stream_set_append_newlines (test_case->log, true); + } suite->debugfs =debugfs_create_dir (suite->name, debugfs_rootdir);
@@ -121,7 +131,7 @@ void kunit_debugfs_destroy_suite(struct kunit_suite *suite)struct kunit_case *test_case;debugfs_remove_recursive (suite->debugfs); -kfree (suite->log); +string_stream_destroy (suite->log);kunit_suite_for_each_test_case (suite, test_case) -kfree (test_case->log); +string_stream_destroy (test_case->log); }
lib/kunit/kunit-test.c +47 -9
94 lines not on this page · raw message
lib/kunit/test.c +5 -39
70 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v6 10/10 kunit: string-stream: Test performance of string_stream
In reply to Richard FitzgeraldAdd a test of the speed and memory use of string_stream.
string_stream_performance_test() doesn't actually "test" anything (it cannot fail unless the system has run out of allocatable memory) but it measures the speed and memory consumption of the string_stream and reports the result.
This allows changes in the string_stream implementation to be compared.
--- lib/kunit/string-stream-test.c | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
lib/kunit/string-stream-test.c +54 -0
@@ -8,7 +8,9 @@#include <kunit/static_stub.h> #include <kunit/test.h> +#include <linux/ktime.h> #include <linux/slab.h> +#include <linux/timekeeping.h> #include "string-stream.h"
@@ -454,6 +456,57 @@ static void string_stream_auto_newline_test(struct kunit *test) "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n"); } +/* +* This doesn't actually "test" anything. It reports time taken +* and memory used for logging a large number of lines. +*/ +static void string_stream_performance_test (struct kunit *test) +{ +struct string_stream_fragment *frag_container; +struct string_stream *stream; +char test_line[101 ]; +ktime_t start_time, end_time; +size_t len, bytes_requested, actual_bytes_used, total_string_length; +int offset, i; + + stream =kunit_alloc_string_stream (test,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stream); + +memset (test_line,'x' ,sizeof (test_line) -1 ); + test_line[sizeof (test_line) -1 ] ='\0' ; + + start_time =ktime_get (); +for (i =0 ; i <10000 ; i++) { + offset = i % (sizeof (test_line) -1 ); +string_stream_add (stream,"%s: %d\n" , &test_line[offset], i); + } + end_time =ktime_get (); + +/* +* Calculate memory used. This doesn't include invisible +* overhead due to kernel allocator fragment size rounding. +*/ + bytes_requested =sizeof (*stream); + actual_bytes_used =ksize (stream); + total_string_length =0 ; + +list_for_each_entry (frag_container, &stream->fragments, node) { + bytes_requested +=sizeof (*frag_container); + actual_bytes_used +=ksize (frag_container); + + len =strlen (frag_container->fragment); + total_string_length += len; + bytes_requested += len +1 ;/* +1 for '\0' */ + actual_bytes_used +=ksize (frag_container->fragment); + } + +kunit_info (test,"Time elapsed: %lld us\n" , +ktime_us_delta (end_time, start_time)); +kunit_info (test,"Total string length: %zu\n" , total_string_length); +kunit_info (test,"Bytes requested: %zu\n" , bytes_requested); +kunit_info (test,"Actual bytes allocated: %zu\n" , actual_bytes_used); +} +static int string_stream_test_init (struct kunit *test) {struct string_stream_test_priv *priv;
@@ -479,6 +532,7 @@ static struct kunit_case string_stream_test_cases[] = {KUNIT_CASE (string_stream_append_empty_string_test),KUNIT_CASE (string_stream_no_auto_newline_test),KUNIT_CASE (string_stream_auto_newline_test), + KUNIT_CASE(string_stream_performance_test), {} };
Signature · 1 line
2.30.2