From: Richard W.M. Jones Date: Fri, 1 May 2009 10:18:53 +0000 (+0100) Subject: Generated code for drop-caches command. X-Git-Tag: 1.0.18~7 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=9222136ac9b2e404dba128b1ac74dacaa8bf1038;p=libguestfs.git Generated code for drop-caches command. --- diff --git a/daemon/actions.h b/daemon/actions.h index d43617b..11253f8 100644 --- a/daemon/actions.h +++ b/daemon/actions.h @@ -110,3 +110,4 @@ extern int do_grub_install (const char *root, const char *device); extern int do_cp (const char *src, const char *dest); extern int do_cp_a (const char *src, const char *dest); extern int do_mv (const char *src, const char *dest); +extern int do_drop_caches (int whattodrop); diff --git a/daemon/stubs.c b/daemon/stubs.c index ddd3fce..64d66a9 100644 --- a/daemon/stubs.c +++ b/daemon/stubs.c @@ -2231,6 +2231,30 @@ done: xdr_free ((xdrproc_t) xdr_guestfs_mv_args, (char *) &args); } +static void drop_caches_stub (XDR *xdr_in) +{ + int r; + struct guestfs_drop_caches_args args; + int whattodrop; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_drop_caches_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "drop_caches"); + return; + } + whattodrop = args.whattodrop; + + r = do_drop_caches (whattodrop); + if (r == -1) + /* do_drop_caches has already called reply_with_error */ + goto done; + + reply (NULL, NULL); +done: + xdr_free ((xdrproc_t) xdr_guestfs_drop_caches_args, (char *) &args); +} + void dispatch_incoming_message (XDR *xdr_in) { switch (proc_nr) { @@ -2501,6 +2525,9 @@ void dispatch_incoming_message (XDR *xdr_in) case GUESTFS_PROC_MV: mv_stub (xdr_in); break; + case GUESTFS_PROC_DROP_CACHES: + drop_caches_stub (xdr_in); + break; default: reply_with_error ("dispatch_incoming_message: unknown procedure number %d", proc_nr); } diff --git a/fish/cmds.c b/fish/cmds.c index 018080d..4529d29 100644 --- a/fish/cmds.c +++ b/fish/cmds.c @@ -67,6 +67,7 @@ void list_commands (void) printf ("%-20s %s\n", "cp-a", "copy a file or directory recursively"); printf ("%-20s %s\n", "debug", "debugging and internals"); printf ("%-20s %s\n", "download", "download a file to the local machine"); + printf ("%-20s %s\n", "drop-caches", "drop kernel page cache, dentries and inodes"); printf ("%-20s %s\n", "exists", "test if file or directory exists"); printf ("%-20s %s\n", "file", "determine file type"); printf ("%-20s %s\n", "fsck", "run the filesystem checker"); @@ -464,6 +465,9 @@ void display_command (const char *cmd) if (strcasecmp (cmd, "mv") == 0) pod2text ("mv - move a file", " mv \n\nThis moves a file from C to C where C is\neither a destination filename or destination directory."); else + if (strcasecmp (cmd, "drop_caches") == 0 || strcasecmp (cmd, "drop-caches") == 0) + pod2text ("drop-caches - drop kernel page cache, dentries and inodes", " drop-caches \n\nThis instructs the guest kernel to drop its page cache,\nand/or dentries and inode caches. The parameter C\ntells the kernel what precisely to drop, see\nL\n\nSetting C to 3 should drop everything.\n\nThis automatically calls L before the operation,\nso that the maximum guest memory is freed."); + else display_builtin_command (cmd); } @@ -2262,6 +2266,20 @@ static int run_mv (const char *cmd, int argc, char *argv[]) return r; } +static int run_drop_caches (const char *cmd, int argc, char *argv[]) +{ + int r; + int whattodrop; + if (argc != 1) { + fprintf (stderr, "%s should have 1 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + whattodrop = atoi (argv[0]); + r = guestfs_drop_caches (g, whattodrop); + return r; +} + int run_action (const char *cmd, int argc, char *argv[]) { if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0) @@ -2585,6 +2603,9 @@ int run_action (const char *cmd, int argc, char *argv[]) if (strcasecmp (cmd, "mv") == 0) return run_mv (cmd, argc, argv); else + if (strcasecmp (cmd, "drop_caches") == 0 || strcasecmp (cmd, "drop-caches") == 0) + return run_drop_caches (cmd, argc, argv); + else { fprintf (stderr, "%s: unknown command\n", cmd); return -1; diff --git a/fish/completion.c b/fish/completion.c index b2f21ff..e2c9780 100644 --- a/fish/completion.c +++ b/fish/completion.c @@ -73,6 +73,7 @@ static const char *const commands[] = { "cp-a", "debug", "download", + "drop-caches", "exists", "file", "fsck", diff --git a/guestfish-actions.pod b/guestfish-actions.pod index c7b9fa4..c31905a 100644 --- a/guestfish-actions.pod +++ b/guestfish-actions.pod @@ -442,6 +442,20 @@ See also C, C. Use C<-> instead of a filename to read/write from stdin/stdout. +=head2 drop-caches + + drop-caches whattodrop + +This instructs the guest kernel to drop its page cache, +and/or dentries and inode caches. The parameter C +tells the kernel what precisely to drop, see +L + +Setting C to 3 should drop everything. + +This automatically calls L before the operation, +so that the maximum guest memory is freed. + =head2 exists exists path diff --git a/guestfs-actions.pod b/guestfs-actions.pod index 1cd4701..b3daa05 100644 --- a/guestfs-actions.pod +++ b/guestfs-actions.pod @@ -576,6 +576,23 @@ See also C, C. This function returns 0 on success or -1 on error. +=head2 guestfs_drop_caches + + int guestfs_drop_caches (guestfs_h *handle, + int whattodrop); + +This instructs the guest kernel to drop its page cache, +and/or dentries and inode caches. The parameter C +tells the kernel what precisely to drop, see +L + +Setting C to 3 should drop everything. + +This automatically calls L before the operation, +so that the maximum guest memory is freed. + +This function returns 0 on success or -1 on error. + =head2 guestfs_exists int guestfs_exists (guestfs_h *handle, diff --git a/java/com/redhat/et/libguestfs/GuestFS.java b/java/com/redhat/et/libguestfs/GuestFS.java index 515fc77..c29789c 100644 --- a/java/com/redhat/et/libguestfs/GuestFS.java +++ b/java/com/redhat/et/libguestfs/GuestFS.java @@ -2487,4 +2487,29 @@ public class GuestFS { private native void _mv (long g, String src, String dest) throws LibGuestFSException; + /** + * drop kernel page cache, dentries and inodes + * + * This instructs the guest kernel to drop its page cache, + * and/or dentries and inode caches. The parameter + * "whattodrop" tells the kernel what precisely to drop, + * see + * + * Setting "whattodrop" to 3 should drop everything. + * + * This automatically calls sync(2) before the operation, + * so that the maximum guest memory is freed. + * + * @throws LibGuestFSException + */ + public void drop_caches (int whattodrop) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("drop_caches: handle is closed"); + _drop_caches (g, whattodrop); + } + private native void _drop_caches (long g, int whattodrop) + throws LibGuestFSException; + } diff --git a/java/com_redhat_et_libguestfs_GuestFS.c b/java/com_redhat_et_libguestfs_GuestFS.c index 0f20d84..ae6ff86 100644 --- a/java/com_redhat_et_libguestfs_GuestFS.c +++ b/java/com_redhat_et_libguestfs_GuestFS.c @@ -2517,3 +2517,19 @@ Java_com_redhat_et_libguestfs_GuestFS__1mv } } +JNIEXPORT void JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1drop_1caches + (JNIEnv *env, jobject obj, jlong jg, jint jwhattodrop) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + int r; + int whattodrop; + + whattodrop = jwhattodrop; + r = guestfs_drop_caches (g, whattodrop); + if (r == -1) { + throw_exception (env, guestfs_last_error (g)); + return ; + } +} + diff --git a/ocaml/guestfs.ml b/ocaml/guestfs.ml index f196ea1..e79a076 100644 --- a/ocaml/guestfs.ml +++ b/ocaml/guestfs.ml @@ -225,3 +225,4 @@ external grub_install : t -> string -> string -> unit = "ocaml_guestfs_grub_inst external cp : t -> string -> string -> unit = "ocaml_guestfs_cp" external cp_a : t -> string -> string -> unit = "ocaml_guestfs_cp_a" external mv : t -> string -> string -> unit = "ocaml_guestfs_mv" +external drop_caches : t -> int -> unit = "ocaml_guestfs_drop_caches" diff --git a/ocaml/guestfs.mli b/ocaml/guestfs.mli index 60ec056..5baf995 100644 --- a/ocaml/guestfs.mli +++ b/ocaml/guestfs.mli @@ -454,3 +454,6 @@ val cp_a : t -> string -> string -> unit val mv : t -> string -> string -> unit (** move a file *) +val drop_caches : t -> int -> unit +(** drop kernel page cache, dentries and inodes *) + diff --git a/ocaml/guestfs_c_actions.c b/ocaml/guestfs_c_actions.c index f4c1808..d6d1305 100644 --- a/ocaml/guestfs_c_actions.c +++ b/ocaml/guestfs_c_actions.c @@ -2942,3 +2942,26 @@ ocaml_guestfs_mv (value gv, value srcv, value destv) CAMLreturn (rv); } +CAMLprim value +ocaml_guestfs_drop_caches (value gv, value whattodropv) +{ + CAMLparam2 (gv, whattodropv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("drop_caches: used handle after closing it"); + + int whattodrop = Int_val (whattodropv); + int r; + + caml_enter_blocking_section (); + r = guestfs_drop_caches (g, whattodrop); + caml_leave_blocking_section (); + if (r == -1) + ocaml_guestfs_raise_error (g, "drop_caches"); + + rv = Val_unit; + CAMLreturn (rv); +} + diff --git a/perl/Guestfs.xs b/perl/Guestfs.xs index 3c5f713..1a45c79 100644 --- a/perl/Guestfs.xs +++ b/perl/Guestfs.xs @@ -1647,3 +1647,14 @@ PREINIT: if (r == -1) croak ("mv: %s", guestfs_last_error (g)); +void +drop_caches (g, whattodrop) + guestfs_h *g; + int whattodrop; +PREINIT: + int r; + PPCODE: + r = guestfs_drop_caches (g, whattodrop); + if (r == -1) + croak ("drop_caches: %s", guestfs_last_error (g)); + diff --git a/perl/lib/Sys/Guestfs.pm b/perl/lib/Sys/Guestfs.pm index 9501a83..63a4925 100644 --- a/perl/lib/Sys/Guestfs.pm +++ b/perl/lib/Sys/Guestfs.pm @@ -461,6 +461,18 @@ C can also be a named pipe. See also C<$h-Eupload>, C<$h-Ecat>. +=item $h->drop_caches ($whattodrop); + +This instructs the guest kernel to drop its page cache, +and/or dentries and inode caches. The parameter C +tells the kernel what precisely to drop, see +L + +Setting C to 3 should drop everything. + +This automatically calls L before the operation, +so that the maximum guest memory is freed. + =item $existsflag = $h->exists ($path); This returns C if and only if there is a file, directory diff --git a/python/guestfs-py.c b/python/guestfs-py.c index b85ba1a..24f9ff8 100644 --- a/python/guestfs-py.c +++ b/python/guestfs-py.c @@ -3156,6 +3156,31 @@ py_guestfs_mv (PyObject *self, PyObject *args) return py_r; } +static PyObject * +py_guestfs_drop_caches (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + int r; + int whattodrop; + + if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_drop_caches", + &py_g, &whattodrop)) + return NULL; + g = get_handle (py_g); + + r = guestfs_drop_caches (g, whattodrop); + if (r == -1) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + Py_INCREF (Py_None); + py_r = Py_None; + return py_r; +} + static PyMethodDef methods[] = { { (char *) "create", py_guestfs_create, METH_VARARGS, NULL }, { (char *) "close", py_guestfs_close, METH_VARARGS, NULL }, @@ -3269,6 +3294,7 @@ static PyMethodDef methods[] = { { (char *) "cp", py_guestfs_cp, METH_VARARGS, NULL }, { (char *) "cp_a", py_guestfs_cp_a, METH_VARARGS, NULL }, { (char *) "mv", py_guestfs_mv, METH_VARARGS, NULL }, + { (char *) "drop_caches", py_guestfs_drop_caches, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/python/guestfs.py b/python/guestfs.py index 291af5d..6c425bc 100644 --- a/python/guestfs.py +++ b/python/guestfs.py @@ -1208,3 +1208,16 @@ class GuestFS: """ return libguestfsmod.mv (self._o, src, dest) + def drop_caches (self, whattodrop): + u"""This instructs the guest kernel to drop its page cache, + and/or dentries and inode caches. The parameter + "whattodrop" tells the kernel what precisely to drop, + see + + Setting "whattodrop" to 3 should drop everything. + + This automatically calls sync(2) before the operation, + so that the maximum guest memory is freed. + """ + return libguestfsmod.drop_caches (self._o, whattodrop) + diff --git a/ruby/ext/guestfs/_guestfs.c b/ruby/ext/guestfs/_guestfs.c index 9d97f96..12ca6de 100644 --- a/ruby/ext/guestfs/_guestfs.c +++ b/ruby/ext/guestfs/_guestfs.c @@ -2661,6 +2661,24 @@ static VALUE ruby_guestfs_mv (VALUE gv, VALUE srcv, VALUE destv) return Qnil; } +static VALUE ruby_guestfs_drop_caches (VALUE gv, VALUE whattodropv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "drop_caches"); + + int whattodrop = NUM2INT (whattodropv); + + int r; + + r = guestfs_drop_caches (g, whattodrop); + if (r == -1) + rb_raise (e_Error, "%s", guestfs_last_error (g)); + + return Qnil; +} + /* Initialize the module. */ void Init__guestfs () { @@ -2891,4 +2909,6 @@ void Init__guestfs () ruby_guestfs_cp_a, 2); rb_define_method (c_guestfs, "mv", ruby_guestfs_mv, 2); + rb_define_method (c_guestfs, "drop_caches", + ruby_guestfs_drop_caches, 1); } diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index 4992355..99f8a44 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -8080,3 +8080,89 @@ int guestfs_mv (guestfs_h *g, return 0; } +struct drop_caches_ctx { + /* This flag is set by the callbacks, so we know we've done + * the callbacks as expected, and in the right sequence. + * 0 = not called, 1 = reply_cb called. + */ + int cb_sequence; + struct guestfs_message_header hdr; + struct guestfs_message_error err; +}; + +static void drop_caches_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct drop_caches_ctx *ctx = (struct drop_caches_ctx *) data; + + /* This should definitely not happen. */ + if (ctx->cb_sequence != 0) { + ctx->cb_sequence = 9999; + error (g, "%s: internal error: reply callback called twice", "guestfs_drop_caches"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_drop_caches"); + return; + } + if (ctx->hdr.status == GUESTFS_STATUS_ERROR) { + if (!xdr_guestfs_message_error (xdr, &ctx->err)) { + error (g, "%s: failed to parse reply error", "guestfs_drop_caches"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_drop_caches (guestfs_h *g, + int whattodrop) +{ + struct guestfs_drop_caches_args args; + struct drop_caches_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_drop_caches") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.whattodrop = whattodrop; + serial = guestfs__send_sync (g, GUESTFS_PROC_DROP_CACHES, + (xdrproc_t) xdr_guestfs_drop_caches_args, (char *) &args); + if (serial == -1) { + guestfs_set_ready (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, drop_caches_reply_cb, &ctx); + (void) ml->main_loop_run (ml, g); + guestfs_set_reply_callback (g, NULL, NULL); + if (ctx.cb_sequence != 1) { + error (g, "%s reply failed, see earlier error messages", "guestfs_drop_caches"); + guestfs_set_ready (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DROP_CACHES, serial) == -1) { + guestfs_set_ready (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + guestfs_set_ready (g); + return -1; + } + + guestfs_set_ready (g); + return 0; +} + diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h index 56088ba..ef4e775 100644 --- a/src/guestfs-actions.h +++ b/src/guestfs-actions.h @@ -129,3 +129,4 @@ extern int guestfs_grub_install (guestfs_h *handle, const char *root, const char extern int guestfs_cp (guestfs_h *handle, const char *src, const char *dest); extern int guestfs_cp_a (guestfs_h *handle, const char *src, const char *dest); extern int guestfs_mv (guestfs_h *handle, const char *src, const char *dest); +extern int guestfs_drop_caches (guestfs_h *handle, int whattodrop); diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c index 6c00afc..395e710 100644 --- a/src/guestfs_protocol.c +++ b/src/guestfs_protocol.c @@ -1508,6 +1508,16 @@ xdr_guestfs_mv_args (XDR *xdrs, guestfs_mv_args *objp) } bool_t +xdr_guestfs_drop_caches_args (XDR *xdrs, guestfs_drop_caches_args *objp) +{ + register int32_t *buf; + + if (!xdr_int (xdrs, &objp->whattodrop)) + return FALSE; + return TRUE; +} + +bool_t xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp) { register int32_t *buf; diff --git a/src/guestfs_protocol.h b/src/guestfs_protocol.h index d2c93b9..a3bf185 100644 --- a/src/guestfs_protocol.h +++ b/src/guestfs_protocol.h @@ -779,6 +779,11 @@ struct guestfs_mv_args { }; typedef struct guestfs_mv_args guestfs_mv_args; +struct guestfs_drop_caches_args { + int whattodrop; +}; +typedef struct guestfs_drop_caches_args guestfs_drop_caches_args; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -869,7 +874,8 @@ enum guestfs_procedure { GUESTFS_PROC_CP = 87, GUESTFS_PROC_CP_A = 88, GUESTFS_PROC_MV = 89, - GUESTFS_PROC_NR_PROCS = 89 + 1, + GUESTFS_PROC_DROP_CACHES = 90, + GUESTFS_PROC_NR_PROCS = 90 + 1, }; typedef enum guestfs_procedure guestfs_procedure; #define GUESTFS_MESSAGE_MAX 4194304 @@ -1041,6 +1047,7 @@ extern bool_t xdr_guestfs_grub_install_args (XDR *, guestfs_grub_install_args*) extern bool_t xdr_guestfs_cp_args (XDR *, guestfs_cp_args*); extern bool_t xdr_guestfs_cp_a_args (XDR *, guestfs_cp_a_args*); extern bool_t xdr_guestfs_mv_args (XDR *, guestfs_mv_args*); +extern bool_t xdr_guestfs_drop_caches_args (XDR *, guestfs_drop_caches_args*); extern bool_t xdr_guestfs_procedure (XDR *, guestfs_procedure*); extern bool_t xdr_guestfs_message_direction (XDR *, guestfs_message_direction*); extern bool_t xdr_guestfs_message_status (XDR *, guestfs_message_status*); @@ -1171,6 +1178,7 @@ extern bool_t xdr_guestfs_grub_install_args (); extern bool_t xdr_guestfs_cp_args (); extern bool_t xdr_guestfs_cp_a_args (); extern bool_t xdr_guestfs_mv_args (); +extern bool_t xdr_guestfs_drop_caches_args (); extern bool_t xdr_guestfs_procedure (); extern bool_t xdr_guestfs_message_direction (); extern bool_t xdr_guestfs_message_status (); diff --git a/src/guestfs_protocol.x b/src/guestfs_protocol.x index 1a6a2d7..68b0ac4 100644 --- a/src/guestfs_protocol.x +++ b/src/guestfs_protocol.x @@ -606,6 +606,10 @@ struct guestfs_mv_args { string dest<>; }; +struct guestfs_drop_caches_args { + int whattodrop; +}; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -696,6 +700,7 @@ enum guestfs_procedure { GUESTFS_PROC_CP = 87, GUESTFS_PROC_CP_A = 88, GUESTFS_PROC_MV = 89, + GUESTFS_PROC_DROP_CACHES = 90, GUESTFS_PROC_NR_PROCS }; diff --git a/tests.c b/tests.c index b47a864..082068a 100644 --- a/tests.c +++ b/tests.c @@ -112,6 +112,34 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_get_e2uuid\" has no tests\n"); } +static int test_drop_caches_0 (void) +{ + /* InitEmpty for drop_caches (0) */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + /* TestRun for drop_caches (0) */ + { + int r; + suppress_error = 0; + r = guestfs_drop_caches (g, 3); + if (r == -1) + return -1; + } + return 0; +} + static int test_mv_0 (void) { /* InitBasicFS for mv (0): create ext2 on /dev/sda1 */ @@ -7235,9 +7263,15 @@ int main (int argc, char *argv[]) exit (1); } - nr_tests = 101; + nr_tests = 102; test_num++; + printf ("%3d/%3d test_drop_caches_0\n", test_num, nr_tests); + if (test_drop_caches_0 () == -1) { + printf ("test_drop_caches_0 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_mv_0\n", test_num, nr_tests); if (test_mv_0 () == -1) { printf ("test_mv_0 FAILED\n");