Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c781626b01b5d94ce653c31a574c1dce3f7323d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2009, 2011, 2012, 2015, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.om.monitor;

import org.eclipse.net4j.internal.util.bundle.OM;
import org.eclipse.net4j.util.CheckUtil;
import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import java.util.Arrays;

/**
 * @author Eike Stepper
 * @since 2.0
 */
public abstract class ProgressDistributor
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_MONITOR, ProgressDistributor.class);

  private double[] distribution;

  public ProgressDistributor()
  {
  }

  public final <CONTEXT> void run(ProgressDistributable<CONTEXT>[] distributables, CONTEXT context, OMMonitor monitor) throws RuntimeException, WrappedException
  {
    double[] distributionCopy;
    synchronized (this)
    {
      if (distribution == null)
      {
        distribution = new double[distributables.length];
        Arrays.fill(distribution, OMMonitor.ONE);
      }
      else
      {
        CheckUtil.checkArg(distribution.length == distributables.length, "distributables.length"); //$NON-NLS-1$
      }

      distributionCopy = new double[distribution.length];
      System.arraycopy(distribution, 0, distributionCopy, 0, distribution.length);
    }

    double total = OMMonitor.ZERO;
    for (int i = 0; i < distributionCopy.length; i++)
    {
      total += distributionCopy[i];
    }

    if (TRACER.isEnabled())
    {
      StringBuilder builder = new StringBuilder("Distribution: "); //$NON-NLS-1$
      for (int i = 0; i < distributionCopy.length; i++)
      {
        builder.append(distributionCopy[i] * OMMonitor.HUNDRED / total);
        builder.append("%, "); //$NON-NLS-1$
      }

      builder.append("("); //$NON-NLS-1$
      builder.append(this);
      builder.append(")"); //$NON-NLS-1$
      TRACER.trace(builder.toString());
    }

    monitor.begin(total);

    try
    {
      double[] times = new double[distributables.length];
      for (int i = 0; i < distributables.length; i++)
      {
        ProgressDistributable<CONTEXT> distributable = distributables[i];
        int count = distributable.getLoopCount(context);
        double work = distributable.getLoopWork(context);

        OMMonitor distributableMonitor = monitor.fork(distributionCopy[i]);
        distributableMonitor.begin(work * count);

        try
        {
          long start = System.currentTimeMillis();
          for (int loop = 0; loop < count; loop++)
          {
            try
            {
              distributable.runLoop(loop, context, distributableMonitor);
            }
            catch (Exception ex)
            {
              throw WrappedException.wrap(ex);
            }
          }

          times[i] = (double)(System.currentTimeMillis() - start) / count;
        }
        finally
        {
          distributableMonitor.done();
        }
      }

      synchronized (this)
      {
        distribute(distribution, times);
      }
    }
    finally
    {
      monitor.done();
    }
  }

  protected abstract void distribute(double[] distribution, double[] times);

  public static <CONTEXT> ProgressDistributable<CONTEXT>[] array(ProgressDistributable<CONTEXT>... ops)
  {
    return ops;
  }

  /**
   * @author Eike Stepper
   */
  public static class Arithmetic extends ProgressDistributor
  {
    private long count;

    private double[] times;

    public Arithmetic()
    {
    }

    @Override
    protected void distribute(double[] distribution, double[] times)
    {
      ++count;
      for (int i = 0; i < times.length; i++)
      {
        this.times[i] += times[i];
        distribution[i] = this.times[i] / count;
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class Geometric extends ProgressDistributor
  {
    public Geometric()
    {
    }

    @Override
    protected void distribute(double[] distribution, double[] times)
    {
      for (int i = 0; i < times.length; i++)
      {
        distribution[i] = (distribution[i] + times[i]) / 2;
      }
    }
  }
}

Back to the top