From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60912) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWqiN-0001G8-Jp for qemu-devel@nongnu.org; Tue, 01 Sep 2015 14:55:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZWqiM-0006Ve-Ji for qemu-devel@nongnu.org; Tue, 01 Sep 2015 14:55:27 -0400 References: <17356f309b182f0df47567aeed40ab0e3cfc99d3.1441127976.git.jcody@redhat.com> From: Eric Blake Message-ID: <55E5F493.9060402@redhat.com> Date: Tue, 1 Sep 2015 12:55:15 -0600 MIME-Version: 1.0 In-Reply-To: <17356f309b182f0df47567aeed40ab0e3cfc99d3.1441127976.git.jcody@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Le0EG7hvEKrp8390Qf1nL9AjvBWqJqgAK" Subject: Re: [Qemu-devel] [RFC PATCH 1/2] util - add automated ID generation utility List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jeff Cody , qemu-devel@nongnu.org Cc: kwolf@redhat.com, qemu-block@nongnu.org, armbru@redhat.com, programmingkidx@gmail.com, jsnow@redhat.com This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --Le0EG7hvEKrp8390Qf1nL9AjvBWqJqgAK Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 09/01/2015 11:23 AM, Jeff Cody wrote: > Multiple sub-systems in QEMU may find it useful to generated IDs > for objects that a user may reference via QMP or HMP. This patch > presents a standardized way to do it, so that automatic ID generation > follows the same rules. >=20 > This patch enforces the following rules when generating an ID: >=20 > 1.) Guarantee no collisions with a user-specified ID > 2.) Identify the sub-system the ID belongs to > 3.) Guarantee of uniqueness > 4.) Spoiling predictibility, to avoid creating an assumption > of object ordering and parsing (i.e., we don't want users to think > they can guess the next ID based on prior behavior). >=20 > The scheme for this is as follows (no spaces): >=20 > # subsys D RR > Reserved char --| | | | > Subsytem String -----| | | s/Subsytem/Subsystem/ > Unique number (64-bit) --| | > Two-digit random number ---| >=20 > For example, a generated node-name for the block sub-system may take th= e > look like this: s/take the// >=20 > #block076 >=20 > The caller of id_generate() is responsible for freeing the generated > node name string with g_free(). >=20 > Signed-off-by: Jeff Cody > --- > include/qemu-common.h | 8 ++++++++ > util/id.c | 35 +++++++++++++++++++++++++++++++++++ > 2 files changed, 43 insertions(+) >=20 > +char *id_generate(IdSubSystems id) > +{ > + const char *id_subsys_str[] =3D { s/id_/const id_/ > + [ID_QDEV] =3D "qdev", > + [ID_BLOCK] =3D "block", > + }; Do we want some sort of compile-time assertion that we have entries for all id values?... > + > + static uint64_t id_counters[ID_MAX]; > + uint32_t rnd; > + > + assert(id < ID_MAX); =2E..maybe in the form of assert(id_subsys_str[id]) > + > + rnd =3D g_random_int_range(0, 99); > + > + return g_strdup_printf("%c%s%" PRIu64 "%" PRId32, ID_SPECIAL_CHAR,= > + id_subsys_str[id= ], > + id_counters[id]+= +, > + rnd); > +} >=20 Looks reasonable to me. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --Le0EG7hvEKrp8390Qf1nL9AjvBWqJqgAK Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJV5fSYAAoJEKeha0olJ0NqaxcH/1c0a3PtbEUWRGcc1i7KeeqI t17EzoduBfNHR9grfeXE98I/K36l5scrO9EomIRt4ik+W1I3+UsDahTlDAaqSJ58 Nw6Mh7v+aVA0O+mZmHyypTKCYikbTvD5HL0DcmHa+MqpJZz7EF1sMb9i6hch+XC+ xSTw9X8B4ZIDOltXpgbTqPEBGRnUgT/Q9AGCAcjBf5NtpddEa0IGy+KFViIDYaux W0VNMH+xLYXDEkeTzTjsDCi7VBVaRN/vb8z/cebiYbEtSTbUiIMupENIMH4hTC0S CSDD2+STDzoKBKoopPgIhu2AKWx3BoWmnR0+BZBB/vk91fs4g3aVMImfbytVIJo= =LIrv -----END PGP SIGNATURE----- --Le0EG7hvEKrp8390Qf1nL9AjvBWqJqgAK--