Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Expose event for 'analysis done' #1149

Merged
merged 6 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/Analysis/Ast/Impl/Analyzer/ActivityTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Microsoft.Python.Analysis.Analyzer {
internal static class ActivityTracker {
private static readonly Dictionary<string, AnalysisState> _modules = new Dictionary<string, AnalysisState>();
private static readonly object _lock = new object();
private static bool _complete;
private static Stopwatch _sw;

private struct AnalysisState {
public int Count;
public bool IsComplete;
}

public static void OnEnqueueModule(string path) {
if (string.IsNullOrEmpty(path)) {
return;
}

lock (_lock) {
if (!_modules.TryGetValue(path, out var st)) {
_modules[path] = default;
} else {
st.IsComplete = false;
}
}
}

public static void OnModuleAnalysisComplete(string path) {
lock (_lock) {
if (_modules.TryGetValue(path, out var st)) {
st.Count++;
st.IsComplete = true;
}
}
}

public static bool IsAnalysisComplete {
get {
lock (_lock) {
return _modules.All(m => m.Value.IsComplete);
}
}
}


public static void StartTracking() {
lock (_lock) {
if (_complete) {
_modules.Clear();
_complete = false;
_sw = Stopwatch.StartNew();
}
}
}

public static void EndTracking() {
lock (_lock) {
_complete = true;
_sw?.Stop();
}
}

public static int ModuleCount {
get {
lock (_lock) {
return _modules.Count;
}
}
}
public static double MillisecondsElapsed => _sw?.Elapsed.TotalMilliseconds ?? 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;

namespace Microsoft.Python.Analysis.Analyzer {
public sealed class AnalysisCompleteEventArgs : EventArgs {
public int ModuleCount { get; }
public double MillisecondsElapsed { get; }

public AnalysisCompleteEventArgs(int moduleCount, double msElapsed) {
ModuleCount = moduleCount;
MillisecondsElapsed = msElapsed;
}
}
}
6 changes: 6 additions & 0 deletions src/Analysis/Ast/Impl/Analyzer/Definitions/IPythonAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -64,5 +65,10 @@ public interface IPythonAnalyzer {
/// Returns list of currently loaded modules.
/// </summary>
IReadOnlyList<IPythonModule> LoadedModules { get; }

/// <summary>
/// Fires when analysis is complete.
/// </summary>
event EventHandler<AnalysisCompleteEventArgs> AnalysisComplete;
}
}
15 changes: 13 additions & 2 deletions src/Analysis/Ast/Impl/Analyzer/PythonAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,22 @@ public void ResetAnalyzer() {
}
}

public IReadOnlyList<IPythonModule> LoadedModules
=> _analysisEntries.Values.ExcludeDefault().Select(v => v.Module).ExcludeDefault().ToArray();
public IReadOnlyList<IPythonModule> LoadedModules {
get {
lock (_syncObj) {
return _analysisEntries.Values.ExcludeDefault().Select(v => v.Module).ExcludeDefault().ToArray();
}
}
}

public event EventHandler<AnalysisCompleteEventArgs> AnalysisComplete;

internal void RaiseAnalysisComplete(int moduleCount, double msElapsed)
=> AnalysisComplete?.Invoke(this, new AnalysisCompleteEventArgs(moduleCount, msElapsed));

private void AnalyzeDocument(AnalysisModuleKey key, PythonAnalyzerEntry entry, ImmutableArray<AnalysisModuleKey> dependencies) {
_analysisCompleteEvent.Reset();
ActivityTracker.StartTracking();
_log?.Log(TraceEventType.Verbose, $"Analysis of {entry.Module.Name}({entry.Module.ModuleType}) queued");

var graphVersion = _dependencyResolver.ChangeValue(key, entry, entry.IsUserModule, dependencies);
Expand Down
12 changes: 11 additions & 1 deletion src/Analysis/Ast/Impl/Analyzer/PythonAnalyzerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,21 @@ private async Task StartAsync() {
stopWatch.Stop();

bool isCanceled;
bool isFinal;
lock (_syncObj) {
isCanceled = _isCanceled;
_state = State.Completed;
isFinal = _walker.MissingKeys.Count == 0 && !isCanceled && remaining == 0;
_walker = null;
}

if (!isCanceled) {
_progress.ReportRemaining(remaining);
if(isFinal) {
ActivityTracker.EndTracking();
(_analyzer as PythonAnalyzer)?.RaiseAnalysisComplete(ActivityTracker.ModuleCount, ActivityTracker.MillisecondsElapsed);
_log?.Log(TraceEventType.Information, $"Analysis complete: {ActivityTracker.ModuleCount} modules in { ActivityTracker.MillisecondsElapsed} ms.");
}
}
}

Expand Down Expand Up @@ -226,6 +233,8 @@ private async Task<int> AnalyzeAffectedEntriesAsync(Stopwatch stopWatch) {
continue;
}

ActivityTracker.OnEnqueueModule(node.Value.Module.FilePath);

if (Interlocked.Increment(ref _runningTasks) >= _maxTaskRunning || _walker.Remaining == 1) {
Analyze(node, null, stopWatch);
} else {
Expand All @@ -241,7 +250,7 @@ private async Task<int> AnalyzeAffectedEntriesAsync(Stopwatch stopWatch) {

if (_walker.MissingKeys.Count == 0 || _walker.MissingKeys.All(k => k.IsTypeshed)) {
Interlocked.Exchange(ref _runningTasks, 0);

if (!isCanceled) {
_analysisCompleteEvent.Set();
}
Expand Down Expand Up @@ -279,6 +288,7 @@ private void Analyze(IDependencyChainNode<PythonAnalyzerEntry> node, AsyncCountd
var startTime = stopWatch.Elapsed;
AnalyzeEntry(entry, module, ast, _walker.Version);
node.Commit();
ActivityTracker.OnModuleAnalysisComplete(node.Value.Module.FilePath);

_log?.Log(TraceEventType.Verbose, $"Analysis of {module.Name}({module.ModuleType}) completed in {(stopWatch.Elapsed - startTime).TotalMilliseconds} ms.");
} catch (OperationCanceledException oce) {
Expand Down
1 change: 1 addition & 0 deletions src/Analysis/Ast/Impl/Dependencies/DependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ private sealed class DependencyChainWalker : IDependencyChainWalker<TKey, TValue
public ImmutableArray<TKey> MissingKeys { get; }
public ImmutableArray<TValue> AffectedValues { get; }
public int Version { get; }
public ImmutableArray<WalkingVertex<TKey, TValue>> Vertices => _startingVertices;

public int Remaining {
get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal interface IDependencyChainWalker<TKey, TValue> {
ImmutableArray<TValue> AffectedValues { get; }
int Version { get; }
int Remaining { get; }

ImmutableArray<WalkingVertex<TKey, TValue>> Vertices { get; }
Task<IDependencyChainNode<TValue>> GetNextAsync(CancellationToken cancellationToken);
}
}
2 changes: 1 addition & 1 deletion src/LanguageServer/Impl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

// #define WAIT_FOR_DEBUGGER
#define WAIT_FOR_DEBUGGER

using System;
using System.Diagnostics;
Expand Down