[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[lvm-devel] [PATCH] percent attribute API for lvm2app (BZ 654441)



Hi,

I am attaching a draft patch for $SUBJ. It is mostly OK in my opinion,
with one catch: it is not clear at all how to deal with multiple public
headers. For now, I have copied over interesting bits of the private
header into the public one, but ultimately, I would like to be able to
have headers from lib/ to be part of the API, so that code can be shared
properly. It would also mean that the public API would come in more than
one header. I am not sure we have any infrastructure for that, right
now.

Anyway, with that caveat, the patch should be more or less OK. It comes
with a "unit" test (the test is still pretty high level, but we still
lack a lot of infrastructure for testing at any lower level; that means
it's not real unit tests, but it's a bit closer than our traditional
functional tests).

Yours,
   Petr

Sun Dec 12 23:48:59 CET 2010  Petr Rockai <me mornfall net>
  * Add a unit test for the float APIs.
Sun Dec 12 21:16:56 CET 2010  Petr Rockai <me mornfall net>
  * Add a missing typedef to lvm2app.h.
Fri Dec  3 17:09:14 CET 2010  Petr Rockai <me mornfall net>
  * Add an implementation of snap_percent_get.
Fri Dec  3 17:05:53 CET 2010  Petr Rockai <me mornfall net>
  * Export bits of the percent functionality in lvm2app.h.
diff -rN -up old-float-api/lib/report/properties.c new-float-api/lib/report/properties.c
--- old-float-api/lib/report/properties.c	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/lib/report/properties.c	2010-12-12 23:50:22.000000000 +0100
@@ -16,6 +16,7 @@
 
 #include "libdevmapper.h"
 #include "properties.h"
+#include "activate.h"
 #include "lvm-logging.h"
 #include "lvm-types.h"
 #include "metadata.h"
@@ -85,6 +86,18 @@ static int _not_implemented_set(void *ob
 	return 0;
 }
 
+static percent_t _copy_percent(const struct logical_volume *lv) {
+    percent_t perc;
+    lv_mirror_percent(lv->vg->cmd, (struct logical_volume *) lv, 0, &perc, NULL);
+    return perc;
+}
+
+static percent_t _snap_percent(const struct logical_volume *lv) {
+    percent_t perc;
+    lv_snapshot_percent(lv, &perc);
+    return perc;
+}
+
 /* PV */
 GET_PV_STR_PROPERTY_FN(pv_fmt, pv_fmt_dup(pv))
 #define _pv_fmt_set _not_implemented_set
@@ -148,9 +161,9 @@ GET_LV_STR_PROPERTY_FN(origin, lv_origin
 #define _origin_set _not_implemented_set
 GET_LV_NUM_PROPERTY_FN(origin_size, lv_origin_size(lv))
 #define _origin_size_set _not_implemented_set
-#define _snap_percent_get _not_implemented_get
+GET_LV_NUM_PROPERTY_FN(snap_percent, _snap_percent(lv))
 #define _snap_percent_set _not_implemented_set
-#define _copy_percent_get _not_implemented_get
+GET_LV_NUM_PROPERTY_FN(copy_percent, _copy_percent(lv))
 #define _copy_percent_set _not_implemented_set
 GET_LV_STR_PROPERTY_FN(move_pv, lv_move_pv_dup(lv->vg->vgmem, lv))
 #define _move_pv_set _not_implemented_set
diff -rN -up old-float-api/liblvm/lvm2app.h new-float-api/liblvm/lvm2app.h
--- old-float-api/liblvm/lvm2app.h	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm2app.h	2010-12-12 23:50:22.000000000 +0100
@@ -1446,8 +1446,10 @@ uint64_t lvm_pv_get_free(const pv_t pv);
  * Name of property to query.  See pvs man page for full list of properties
  * that may be queried.
  *
- * The memory allocated for a string property value is tied to the vg_t
- * handle and will be released when lvm_vg_close() is called.
+ * The memory allocated for a string property value is tied to the vg_t handle
+ * and will be released when lvm_vg_close() is called. For "percent" values
+ * (those obtained for copy_percent and snap_percent properties), please see
+ * percent_range_t and lvm_percent_to_float().
  *
  * Example:
  *      lvm_property_value value;
@@ -1578,6 +1580,30 @@ pv_t lvm_pv_from_uuid(vg_t vg, const cha
  */
 int lvm_pv_resize(const pv_t pv, uint64_t new_size);
 
+#ifndef _LVM_PERCENT_H
+
+/**
+ * This type defines a couple of special percent values. The PERCENT_0 and
+ * PERCENT_100 constants designate *exact* percentages: values are never
+ * rounded to either of these two.
+ */
+typedef enum {
+	PERCENT_0 = 0,
+	PERCENT_1 = 1000000,
+	PERCENT_100 = 100 * PERCENT_1,
+	PERCENT_INVALID = -1
+} percent_range_t;
+
+typedef int32_t percent_t;
+
+#endif
+
+/**
+ * Convert a (fixed-point) value obtained from the percent-denominated
+ * *_get_property functions into a floating-point value.
+ */
+float lvm_percent_to_float(percent_t v);
+
 #ifdef __cplusplus
 }
 #endif
diff -rN -up old-float-api/liblvm/lvm_base.c new-float-api/liblvm/lvm_base.c
--- old-float-api/liblvm/lvm_base.c	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm_base.c	2010-12-12 23:50:22.000000000 +0100
@@ -13,11 +13,11 @@
  */
 
 #include "lib.h"
-#include "lvm2app.h"
 #include "toolcontext.h"
 #include "locking.h"
 #include "lvm-version.h"
 #include "metadata-exported.h"
+#include "lvm2app.h"
 
 const char *lvm_library_get_version(void)
 {
diff -rN -up old-float-api/liblvm/lvm_lv.c new-float-api/liblvm/lvm_lv.c
--- old-float-api/liblvm/lvm_lv.c	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm_lv.c	2010-12-12 23:50:22.000000000 +0100
@@ -13,7 +13,6 @@
  */
 
 #include "lib.h"
-#include "lvm2app.h"
 #include "metadata.h"
 #include "lvm-string.h"
 #include "defaults.h"
@@ -21,6 +20,7 @@
 #include "locking.h"
 #include "activate.h"
 #include "lvm_misc.h"
+#include "lvm2app.h"
 
 static int _lv_check_handle(const lv_t lv, const int vg_writeable)
 {
diff -rN -up old-float-api/liblvm/lvm_misc.c new-float-api/liblvm/lvm_misc.c
--- old-float-api/liblvm/lvm_misc.c	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm_misc.c	2010-12-12 23:50:22.000000000 +0100
@@ -12,10 +12,10 @@
  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "lvm2app.h"
-#include "lvm_misc.h"
 #include "lib.h"
 #include "properties.h"
+#include "lvm_misc.h"
+#include "lvm2app.h"
 
 struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list)
 {
diff -rN -up old-float-api/liblvm/lvm_misc.h new-float-api/liblvm/lvm_misc.h
--- old-float-api/liblvm/lvm_misc.h	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm_misc.h	2010-12-12 23:50:22.000000000 +0100
@@ -15,6 +15,7 @@
 #define _LVM2APP_MISC_H
 
 #include "libdevmapper.h"
+#include "lvm2app.h"
 
 struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list);
 struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
diff -rN -up old-float-api/liblvm/lvm_pv.c new-float-api/liblvm/lvm_pv.c
--- old-float-api/liblvm/lvm_pv.c	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm_pv.c	2010-12-12 23:50:22.000000000 +0100
@@ -13,10 +13,10 @@
  */
 
 #include "lib.h"
-#include "lvm2app.h"
 #include "metadata.h"
 #include "lvm-string.h"
 #include "lvm_misc.h"
+#include "lvm2app.h"
 
 const char *lvm_pv_get_uuid(const pv_t pv)
 {
diff -rN -up old-float-api/liblvm/lvm_vg.c new-float-api/liblvm/lvm_vg.c
--- old-float-api/liblvm/lvm_vg.c	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/liblvm/lvm_vg.c	2010-12-12 23:50:22.000000000 +0100
@@ -13,13 +13,13 @@
  */
 
 #include "lib.h"
-#include "lvm2app.h"
 #include "toolcontext.h"
 #include "metadata.h"
 #include "archiver.h"
 #include "locking.h"
 #include "lvmcache.h"
 #include "lvm_misc.h"
+#include "lvm2app.h"
 
 int lvm_vg_add_tag(vg_t vg, const char *tag)
 {
diff -rN -up old-float-api/test/api/Makefile.in new-float-api/test/api/Makefile.in
--- old-float-api/test/api/Makefile.in	2010-12-12 23:50:22.000000000 +0100
+++ new-float-api/test/api/Makefile.in	2010-12-12 23:50:22.000000000 +0100
@@ -24,7 +24,7 @@ test_SOURCES = test.c
 wrapper_SOURCES = test.c
 INCLUDES += -I../../include
 
-UNIT = vgtest.t
+UNIT = vgtest.t percent.t
 
 LVMLIBS = @LVM2APP_LIB@ -ldevmapper
 DEPLIBS = $(top_builddir)/liblvm/liblvm2app.so $(top_builddir)/libdm/libdevmapper.so
diff -rN -up old-float-api/test/api/percent.c new-float-api/test/api/percent.c
--- old-float-api/test/api/percent.c	1970-01-01 01:00:00.000000000 +0100
+++ new-float-api/test/api/percent.c	2010-12-12 23:50:22.000000000 +0100
@@ -0,0 +1,45 @@
+#include "lvm2app.h"
+
+#define assert(x) do { if (!(x)) goto bad; } while (0)
+
+int main(int argc, char *argv[])
+{
+        lvm_t handle = lvm_init(NULL);
+        assert(handle);
+
+	vg_t vg = lvm_vg_open(handle, argv[1], "r", 0);
+        assert(vg);
+
+        lv_t lv = lvm_lv_from_name(vg, "snap");
+        assert(lv);
+
+        struct lvm_property_value v = lvm_lv_get_property(lv, "snap_percent");
+        assert(v.is_valid);
+        assert(v.value.integer == PERCENT_0);
+
+        lv = lvm_lv_from_name(vg, "mirr");
+        assert(lv);
+
+        v = lvm_lv_get_property(lv, "copy_percent");
+        assert(v.is_valid);
+        assert(v.value.integer == PERCENT_100);
+
+        lv = lvm_lv_from_name(vg, "snap2");
+        assert(lv);
+
+        v = lvm_lv_get_property(lv, "snap_percent");
+        assert(v.is_valid);
+        assert(v.value.integer == 50 * PERCENT_1);
+
+        lvm_vg_close(vg);
+        return 0;
+
+bad:
+	if (handle && lvm_errno(handle))
+		fprintf(stderr, "LVM Error: %s\n", lvm_errmsg(handle));
+	if (vg)
+		lvm_vg_close(vg);
+	if (handle)
+		lvm_quit(handle);
+	return 1;
+}
diff -rN -up old-float-api/test/api/percent.sh new-float-api/test/api/percent.sh
--- old-float-api/test/api/percent.sh	1970-01-01 01:00:00.000000000 +0100
+++ new-float-api/test/api/percent.sh	2010-12-12 23:50:22.000000000 +0100
@@ -0,0 +1,10 @@
+. ./test-utils.sh
+aux prepare_devs 2
+vgcreate -c n -s 4k $vg $devs
+lvcreate -n foo $vg -l 5
+lvcreate -s -n snap $vg/foo -l 2 -c 4k
+lvcreate -s -n snap2 $vg/foo -l 6 -c 4k
+dd if=/dev/urandom of=$DM_DEV_DIR/$vg/snap2 count=1 bs=1024
+lvcreate -m 1 -n mirr $vg -l 1 --mirrorlog core
+lvs
+apitest percent $vg

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]