Skip to content

Instantly share code, notes, and snippets.

@mitchhentges
Last active February 18, 2016 16:49
Show Gist options
  • Save mitchhentges/db270f2749d4bbc7aeca to your computer and use it in GitHub Desktop.
Save mitchhentges/db270f2749d4bbc7aeca to your computer and use it in GitHub Desktop.
Calculating timings for a sequential-lock-by-lock algorithm
#include <stdio.h>
#include <stdlib.h>
int compute(int, int);
int main(int argc, char* argv[])
{
printf("\t distributed-lock-timings\n");
printf("Will calculate total time (in units of '1' per 'operation', where 'operation' is requesting a lock, performing transaction, or removing a lock)\n");
printf("These timings are based on two concurrent requests: one trying to change three resources, the other trying to change just one\n");
printf("It is assume that each lock is being acquired sequentially, per request\n");
printf("The requests are running in parallel (other than when blocked by resource contention, of course\n");
printf("\n");
printf("The resources are A, B, and C. The \"big\" request will lock in the following order: [A, B, C]\n");
printf("c: The average time it takes to complete the two requests\n");
printf("w: The total time that one of the requests was suspended (due to resource contention)\n");
printf("n: Which resource that the small request going to affect, numbered in the order that it is locked by the big request: A=0, B=1, C=2 (A is locked first, so is 0)\n");
printf("t: offset between small request starting and big request starting\n");
printf("\n\n");
printf("*--*--*-------*-------*\n");
printf("| n| t| c| w|\n");
for (int i = 0; i < 3; i++)
{
printf("*--*--*-------*-------*\n");
printf("|%2d| | | |\n", i);
printf("*--*--*-------*-------*\n");
for (int j = -4; j < 8; j++)
{
printf("|%2d|%2d|%7.2f|%7.2f|\n", i, j, (7 + 3 + compute(i, j)) / 2.0f, compute(i, j) / 2.0f);
}
}
printf("*--*--*-------*-------*\n");
}
int compute(int n, int t)
{
int toReturn = (t > n)
? 7 - t - n
: 3 + t - n;
return (toReturn < 0) ? 0 : toReturn;
}
@mitchhentges
Copy link
Author

Current output:

     distributed-lock-timings
Will calculate total time (in units of '1' per 'operation', where 'operation' is requesting a lock, performing transaction, or removing a lock)
These timings are based on two concurrent requests: one trying to change three resources, the other trying to change just one
It is assume that each lock is being acquired sequentially, per request
The requests are running in parallel (other than when blocked by resource contention, of course

The resources are A, B, and C. The "big" request will lock in the following order: [A, B, C]
c: The average time it takes to complete the two requests
w: The total time that one of the requests was suspended (due to resource contention)
n: Which resource that the small request going to affect, numbered in the order that it is locked by the big request: A=0, B=1, C=2 (A is locked first, so is 0)
t: offset between small request starting and big request starting


*--*--*-------*-------*
| n| t|      c|      w|
*--*--*-------*-------*
| 0|  |       |       |
*--*--*-------*-------*
| 0|-4|   5.00|   0.00|
| 0|-3|   5.00|   0.00|
| 0|-2|   5.50|   0.50|
| 0|-1|   6.00|   1.00|
| 0| 0|   6.50|   1.50|
| 0| 1|   8.00|   3.00|
| 0| 2|   7.50|   2.50|
| 0| 3|   7.00|   2.00|
| 0| 4|   6.50|   1.50|
| 0| 5|   6.00|   1.00|
| 0| 6|   5.50|   0.50|
| 0| 7|   5.00|   0.00|
*--*--*-------*-------*
| 1|  |       |       |
*--*--*-------*-------*
| 1|-4|   5.00|   0.00|
| 1|-3|   5.00|   0.00|
| 1|-2|   5.00|   0.00|
| 1|-1|   5.50|   0.50|
| 1| 0|   6.00|   1.00|
| 1| 1|   6.50|   1.50|
| 1| 2|   7.00|   2.00|
| 1| 3|   6.50|   1.50|
| 1| 4|   6.00|   1.00|
| 1| 5|   5.50|   0.50|
| 1| 6|   5.00|   0.00|
| 1| 7|   5.00|   0.00|
*--*--*-------*-------*
| 2|  |       |       |
*--*--*-------*-------*
| 2|-4|   5.00|   0.00|
| 2|-3|   5.00|   0.00|
| 2|-2|   5.00|   0.00|
| 2|-1|   5.00|   0.00|
| 2| 0|   5.50|   0.50|
| 2| 1|   6.00|   1.00|
| 2| 2|   6.50|   1.50|
| 2| 3|   6.00|   1.00|
| 2| 4|   5.50|   0.50|
| 2| 5|   5.00|   0.00|
| 2| 6|   5.00|   0.00|
| 2| 7|   5.00|   0.00|
*--*--*-------*-------*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment