flexnbd status: Add the size of the backing file, in bytes

This will be handy information if you're querying flexnbd for migration
stats, particularly.
This commit is contained in:
nick
2013-07-08 10:11:18 +01:00
parent f556f298b1
commit dee0bb27d6
3 changed files with 52 additions and 5 deletions

View File

@@ -9,6 +9,7 @@ struct status * status_create( struct server * serve )
status = xmalloc( sizeof( struct status ) ); status = xmalloc( sizeof( struct status ) );
status->pid = getpid(); status->pid = getpid();
status->size = serve->size;
status->has_control = serve->success; status->has_control = serve->success;
status->is_mirroring = NULL != serve->mirror; status->is_mirroring = NULL != serve->mirror;
@@ -25,10 +26,13 @@ struct status * status_create( struct server * serve )
do{dprintf( fd, #var "=%s ", BOOL_S( status->var ) );}while(0) do{dprintf( fd, #var "=%s ", BOOL_S( status->var ) );}while(0)
#define PRINT_INT( var ) \ #define PRINT_INT( var ) \
do{dprintf( fd, #var "=%d ", status->var );}while(0) do{dprintf( fd, #var "=%d ", status->var );}while(0)
#define PRINT_UINT64( var ) \
do{dprintf( fd, #var "=%"PRIu64" ", status->var );}while(0)
int status_write( struct status * status, int fd ) int status_write( struct status * status, int fd )
{ {
PRINT_INT( pid ); PRINT_INT( pid );
PRINT_UINT64( size );
PRINT_BOOL( is_mirroring ); PRINT_BOOL( is_mirroring );
PRINT_BOOL( has_control ); PRINT_BOOL( has_control );

View File

@@ -20,6 +20,9 @@
* pid: * pid:
* The current process ID. * The current process ID.
* *
* size:
* The size of the backing file being served, in bytes.
*
* has_control: * has_control:
* This will be false when the server is listening for an incoming * This will be false when the server is listening for an incoming
* migration. It will switch to true when the end-of-migration * migration. It will switch to true when the end-of-migration
@@ -53,6 +56,7 @@
struct status { struct status {
pid_t pid; pid_t pid;
uint64_t size;
int has_control; int has_control;
int is_mirroring; int is_mirroring;
int migration_pass; int migration_pass;

View File

@@ -5,12 +5,17 @@
#include <check.h> #include <check.h>
void prepare_server( struct server* serve )
{
serve->mirror = NULL;
}
START_TEST( test_status_create ) START_TEST( test_status_create )
{ {
struct server server; struct server server;
struct status *status = NULL; struct status *status = NULL;
server.mirror = NULL; prepare_server( &server );
status = status_create( &server ); status = status_create( &server );
fail_if( NULL == status, "Status wasn't allocated" ); fail_if( NULL == status, "Status wasn't allocated" );
@@ -23,7 +28,7 @@ START_TEST( test_gets_has_control )
struct server server; struct server server;
struct status * status; struct status * status;
server.mirror = NULL; prepare_server( &server );
server.success = 1; server.success = 1;
status = status_create( &server ); status = status_create( &server );
@@ -38,7 +43,7 @@ START_TEST( test_gets_is_mirroring )
struct server server; struct server server;
struct status * status; struct status * status;
server.mirror = NULL; prepare_server( &server );
status = status_create( &server ); status = status_create( &server );
fail_if( status->is_mirroring, "is_mirroring was set" ); fail_if( status->is_mirroring, "is_mirroring was set" );
status_destroy( status ); status_destroy( status );
@@ -55,7 +60,7 @@ START_TEST( test_gets_migration_pass )
struct server server; struct server server;
struct status * status; struct status * status;
server.mirror = NULL; prepare_server( &server );
status = status_create( &server ); status = status_create( &server );
fail_if( status->migration_pass != 0, "migration_pass was set" ); fail_if( status->migration_pass != 0, "migration_pass was set" );
status_destroy( status ); status_destroy( status );
@@ -75,7 +80,7 @@ START_TEST( test_gets_pid )
struct server server; struct server server;
struct status * status; struct status * status;
server.mirror = NULL; prepare_server( &server );
status = status_create( &server ); status = status_create( &server );
fail_unless( getpid() == status->pid, "Pid wasn't gathered" ); fail_unless( getpid() == status->pid, "Pid wasn't gathered" );
@@ -84,6 +89,21 @@ START_TEST( test_gets_pid )
} }
END_TEST END_TEST
START_TEST( test_gets_size )
{
struct server server;
struct status * status;
prepare_server( &server );
server.size = 1024;
status = status_create( &server );
fail_unless( 1024 == status->size, "Size wasn't gathered" );
status_destroy( status );
}
END_TEST
START_TEST( test_renders_has_control ) START_TEST( test_renders_has_control )
{ {
@@ -158,6 +178,23 @@ START_TEST( test_renders_pid )
} }
END_TEST END_TEST
START_TEST( test_renders_size )
{
struct status status;
int fds[2];
pipe(fds);
char buf[1024] = {0};
status.size = ( (uint64_t)1 << 33 );
status_write( &status, fds[1] );
fail_unless( read_until_newline( fds[0], buf, 1024 ) > 0,
"Couldn't read the result" );
char *found = strstr( buf, "size=8589934592" );
fail_if( NULL == found, "size=8589934592 not found" );
}
END_TEST
START_TEST( test_renders_migration_pass ) START_TEST( test_renders_migration_pass )
{ {
struct status status; struct status status;
@@ -199,11 +236,13 @@ Suite *status_suite(void)
tcase_add_test(tc_create, test_gets_has_control); tcase_add_test(tc_create, test_gets_has_control);
tcase_add_test(tc_create, test_gets_is_mirroring); tcase_add_test(tc_create, test_gets_is_mirroring);
tcase_add_test(tc_create, test_gets_pid); tcase_add_test(tc_create, test_gets_pid);
tcase_add_test(tc_create, test_gets_size);
tcase_add_test(tc_create, test_gets_migration_pass); tcase_add_test(tc_create, test_gets_migration_pass);
tcase_add_test(tc_render, test_renders_has_control); tcase_add_test(tc_render, test_renders_has_control);
tcase_add_test(tc_render, test_renders_is_mirroring); tcase_add_test(tc_render, test_renders_is_mirroring);
tcase_add_test(tc_render, test_renders_pid); tcase_add_test(tc_render, test_renders_pid);
tcase_add_test(tc_render, test_renders_size);
tcase_add_test(tc_render, test_renders_migration_pass); tcase_add_test(tc_render, test_renders_migration_pass);
suite_add_tcase(s, tc_create); suite_add_tcase(s, tc_create);