qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@gmail.com>
To: Alberto Garcia <berto@igalia.com>
Cc: "Kevin Wolf" <kwolf@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	qemu-devel@nongnu.org, "Benoît Canet" <benoit.canet@nodalink.com>
Subject: Re: [Qemu-devel] [PATCH 2/9] throttle: Add throttle group infrastructure
Date: Tue, 3 Mar 2015 10:38:45 -0600	[thread overview]
Message-ID: <20150303163845.GD15846@stefanha-thinkpad.redhat.com> (raw)
In-Reply-To: <06fbcac6a26a64cce978e54195452846496323c6.1423842044.git.berto@igalia.com>

[-- Attachment #1: Type: text/plain, Size: 4943 bytes --]

On Fri, Feb 13, 2015 at 06:06:10PM +0200, Alberto Garcia wrote:
> From: Benoît Canet <benoit.canet@nodalink.com>
> 
> The throttle_group_incref increment the refcount of a throttle group given it's

s/it's/its/
http://www.its-not-its.info/

> +typedef struct ThrottleGroup {
> +    char name[32];
> +    ThrottleState ts;
> +    uint64_t refcount;
> +    QTAILQ_ENTRY(ThrottleGroup) list;
> +    QLIST_HEAD(, BlockDriverState) head;
> +    BlockDriverState *tokens[2]; /* current round-robin tokens */
> +    QemuMutex lock; /* Used to synchronize all elements belonging to a group */

Not sure what this comment means.  Which fields are protected by lock?

I think refcount is not protected by lock, since it is incremented in
throttle_group_incref() without holding the lock.

> +} ThrottleGroup;
> +
> +static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
> +    QTAILQ_HEAD_INITIALIZER(throttle_groups);

Is throttle_groups protected by the QEMU global mutex?  It would be
helpful to add a comment.

> +
> +/* increments a ThrottleGroup reference count given it's name

s/it's/its/

> + *
> + * If no ThrottleGroup is found with the given name a new one is created.
> + *
> + * @name: the name of the ThrottleGroup
> + * @ret:  the ThrottleGroup's ThrottleState address
> + */
> +ThrottleState *throttle_group_incref(const char *name)
> +{
> +    ThrottleGroup *tg;
> +
> +    /* return the correct ThrottleState if a group with this name exists */
> +    QTAILQ_FOREACH(tg, &throttle_groups, list) {
> +        /* group not found -> continue */
> +        if (strcmp(name, tg->name)) {
> +            continue;
> +        }
> +        /* group found -> increment it's refcount and return ThrottleState */
> +        tg->refcount++;
> +        return &tg->ts;
> +    }
> +
> +    /* throttle group not found -> prepare new entry */
> +    tg = g_new0(ThrottleGroup, 1);
> +    pstrcpy(tg->name, sizeof(tg->name), name);

Silently truncating to 32 chars is confusing.  Please use g_strdup() and
g_free() the string when refcount reaches 0.

> +    qemu_mutex_init(&tg->lock);
> +    throttle_init(&tg->ts);
> +    QLIST_INIT(&tg->head);
> +    tg->refcount = 1;
> +
> +    /* insert new entry in the list */
> +    QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);

It is safest to hold tg->lock before adding the group to the list.  This
way there is a memory barrier and other threads will not access the
group until we've finished adding it to the list.

The memory barrier is important so other threads don't see old memory
values for the group's fields.

> +
> +    /* return newly allocated ThrottleState */
> +    return &tg->ts;
> +}
> +
> +/* decrement a ThrottleGroup given it's ThrottleState address

s/it's/its/

> + *
> + * When the refcount reach zero the ThrottleGroup is destroyed
> + *
> + * @ts:  The ThrottleState address belonging to the ThrottleGroup to unref
> + * @ret: true on success else false
> + */
> +bool throttle_group_unref(ThrottleState *ts)
> +{
> +    ThrottleGroup *tg;
> +    bool found = false;
> +
> +    /* Find the ThrottleGroup of the given ThrottleState */
> +    QTAILQ_FOREACH(tg, &throttle_groups, list) {
> +        /* correct group found stop iterating */
> +        if (&tg->ts == ts) {
> +            qemu_mutex_lock(&tg->lock);
> +            found = true;
> +            break;
> +        }
> +    }
> +
> +    /* If the ThrottleState was not found something is seriously broken */
> +    if (!found) {
> +        return false;
> +    }

Please correct me if I'm wrong but I suggest:

Make this function void and replace this statement with assert(found).
This case should never happen and I doubt callers will be able to handle
the error case.

> +/* Compare a name with a given ThrottleState group name
> + *
> + * @ts:   the throttle state whose group we are inspecting
> + * @name: the name to compare
> + * @ret:  true if names are equal else false
> + */
> +bool throttle_group_compare(ThrottleState *ts, const char *name)

Normally a plain "compare" function checks if two values of the same
type are equal.

The name throttle_group_compare_name() would be clearer.

> +{
> +    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);

Why can this function use container_of() while throttle_group_unref()
has to loop over all ThrottleGroups to find ts?

> +/* Used to lock a ThrottleState's ThrottleGroup
> + *
> + * @ts:     the ThrottleState the code is working on
> + */
> +void throttle_group_lock(ThrottleState *ts)
> +{
> +    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
> +    qemu_mutex_lock(&tg->lock);
> +}

lock/unlock functions are sometimes an indication that the calling code
should really be moved into this file.  I'm not sure yet since I haven't
read all the patches, but it is a little suspicious.

[-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --]

  reply	other threads:[~2015-03-03 16:38 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-13 16:06 [Qemu-devel] [PATCH v2 0/9] Block Throttle Group Support Alberto Garcia
2015-02-13 16:06 ` [Qemu-devel] [PATCH 1/9] throttle: Extract timers from ThrottleState into a separate ThrottleTimers structure Alberto Garcia
2015-03-03 16:03   ` Stefan Hajnoczi
2015-02-13 16:06 ` [Qemu-devel] [PATCH 2/9] throttle: Add throttle group infrastructure Alberto Garcia
2015-03-03 16:38   ` Stefan Hajnoczi [this message]
2015-03-04 10:18     ` Alberto Garcia
2015-03-04 16:02       ` Stefan Hajnoczi
2015-02-13 16:06 ` [Qemu-devel] [PATCH 3/9] throttle: Add throttle group infrastructure tests Alberto Garcia
2015-02-13 16:06 ` [Qemu-devel] [PATCH 4/9] throttle: Prepare to have multiple timers for one ThrottleState Alberto Garcia
2015-03-03 16:50   ` Stefan Hajnoczi
2015-02-13 16:06 ` [Qemu-devel] [PATCH 5/9] throttle: Add a way to know if throttle_schedule_timer had armed a timer Alberto Garcia
2015-02-13 16:06 ` [Qemu-devel] [PATCH 6/9] throttle: Add a way to fire one of the timers asap like a bottom half Alberto Garcia
2015-03-03 17:08   ` Stefan Hajnoczi
2015-02-13 16:06 ` [Qemu-devel] [PATCH 7/9] throttle: Add throttle group support Alberto Garcia
2015-02-24 16:45   ` Eric Blake
2015-02-24 16:47     ` Eric Blake
2015-03-03 21:00   ` Stefan Hajnoczi
2015-03-04 13:53     ` Alberto Garcia
2015-03-04 16:04       ` Stefan Hajnoczi
2015-03-04 16:16         ` Alberto Garcia
2015-03-05 17:41           ` Stefan Hajnoczi
2015-02-13 16:06 ` [Qemu-devel] [PATCH 8/9] throttle: Update throttle infrastructure copyright Alberto Garcia
2015-02-24 16:49   ` Eric Blake
2015-02-24 20:21     ` Benoît Canet
2015-02-13 16:06 ` [Qemu-devel] [PATCH 9/9] throttle: add name of ThrottleGroup to BlockDeviceInfo Alberto Garcia
2015-02-24 16:54   ` Eric Blake
2015-02-25 10:56     ` Alberto Garcia
2015-02-25 15:23       ` Eric Blake
2015-02-25 15:37         ` Alberto Garcia
2015-02-26 13:56         ` Alberto Garcia
2015-03-03 17:53           ` Eric Blake
2015-03-04  7:09             ` Markus Armbruster
2015-03-04  7:20               ` Alberto Garcia
2015-03-03 21:07 ` [Qemu-devel] [PATCH v2 0/9] Block Throttle Group Support Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150303163845.GD15846@stefanha-thinkpad.redhat.com \
    --to=stefanha@gmail.com \
    --cc=benoit.canet@nodalink.com \
    --cc=berto@igalia.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).