todo: Add note about using blktrace.
[libguestfs.git] / ocaml / t / guestfs_070_threads.ml
1 (* libguestfs OCaml bindings
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *)
18
19 open Unix
20
21 (* Start a background thread which does lots of allocation and
22  * GC activity.
23  *)
24 let thread = Thread.create (
25   fun () ->
26     while true do
27       Gc.compact ();
28       ignore (Array.init 1000 (fun i -> Thread.yield (); String.create (8*i)))
29     done
30 ) ()
31
32 let () =
33   let g = Guestfs.create () in
34
35   let fd = openfile "test.img" [O_WRONLY;O_CREAT;O_NOCTTY;O_TRUNC] 0o666 in
36   ftruncate fd (500 * 1024 * 1024);
37   close fd;
38
39   (* Copy these strings so they're located on the heap and
40    * subject to garbage collection.
41    *)
42   let s = String.copy "test.img" in
43   Guestfs.add_drive_ro g s;
44   Guestfs.launch g;
45
46   let dev = String.copy "/dev/sda" in
47   Guestfs.pvcreate g dev;
48   let vg = String.copy "VG" in
49   Guestfs.vgcreate g vg [|dev|];
50   let s = String.copy "LV1" in
51   Guestfs.lvcreate g s vg 200;
52   let s = String.copy "LV2" in
53   Guestfs.lvcreate g s vg 200;
54
55   let lvs = Guestfs.lvs g in
56   if lvs <> [|"/dev/VG/LV1"; "/dev/VG/LV2"|] then
57     failwith "Guestfs.lvs returned incorrect result";
58
59   let s = String.copy "ext3" in
60   let lv = String.copy "/dev/VG/LV1" in
61   Guestfs.mkfs g s lv;
62   let s = String.copy "/" in
63   Guestfs.mount_options g "" lv s;
64   let s = String.copy "/test" in
65   Guestfs.touch g s;
66
67   Guestfs.umount_all g;
68   Guestfs.sync g;
69   Guestfs.close g;
70   unlink "test.img";
71   Gc.compact ();
72   exit 0